-5
$\begingroup$

I am a Java developer building a web app that I will be deploying "in the cloud" (I hate that expression) in a few months. I'm trying to develop a function that will let me spawn and kill the right amount of "computing horsepower" (virtual machines) depending on the current load of the system (by "load" I mean traffic, # of users, etc.).

I have developed such a function, but because it's been so long since I cracked open any of my college mmathbooks, there's one component of it that I'm struggling with that could/should be re-written as it is a piecewise function.

Basically, I have an "outer"/main function c that maps a real (the load) to an integer (# of virtual machines I should have running to support the load):

c : L in the domain of reals --> z in the domain of integers c(L) = A * t(L) 

Where c(L) is the function that my Java code will call, L is a measure of the current load on the system (of type double since load could be 5.0 or 34.2094, etc.), A is a fairly-complicated function that doesn't concern this question (so I omitted it), and t(L) is the piecewise function I need help re-writing:

t(L) = {    0 : if L < 1        {    L+1 : if L is an integer // 5.0 == 5        {    ceil(L) : if L is not an integer // 5.5 

I understand that the first and third piecewise rules sort of contradict each other, but the first one (0 : if L <1) trumps/overrides the last one (ceil(L) : if L not integer).

So, if L is less than 1, I need t(L) to always return 0. Otherwise, if its an integer, I need it to return L+1, or ceil(L) for any other reals.

So, some examples:

t(0.5) = 0 t(1) = 2 // 1 + 1 -> 2 t(1.5) = 2 // ceil(1.5) -> 2 t(2) = 3 t(57.39854) = 58 

Note: L will always be non-negative (0+).

For the life of me I can't figure out how to re-write t(L) in such a way as to be "in-line" or normal (not containing if-else piecewise rules). Thanks for any and all help here!

  • 1
    The accounts `Adam Tannon` and `zharvey` have been merged.2012-07-13

2 Answers 2

5

Would

L < 1 ? 0 : (int)L+1 

be streamlined enough for you? I don't think there's any simpler way to write your function in Java.

Yes, this expression uses the ternary conditional operator ?:, which is essentially equivalent to an if–else clause except that it switches between expressions instead of blocks. Depending on how smart your compiler is, the resulting assembly code may or may not end up involving an actual conditional jump.

Still, I very much doubt that this expression will be the performance bottleneck in your application. In fact, if it's really going to be part of a load balancer, I doubt it will even make a measurable difference at all — if the calculation is done, say, once per second, a few nanoseconds lost to a branch misprediction will be like a drop in the ocean. Unless this code really makes up a major part of the inner work loop of your program, micro-optimizing it like this probably violates Knuth's rule on premature optimization:

"Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."

  • 3
    @zharvey: But what is the problem with the function being piecewise?2012-07-09
0

I figured it out:

t(L) = floor((3L+1)/(2L+2)) + floor(L) 

Try it out:

t(0.6) = 0 t(1) = 2 t(1.6) = 2 t(5.8) = 6 t(50) = 51 

Doesn't work when L < 0, but like I said that use case will never occur because L will always be non-negative. Achieves exactly what I asked as my original question.

  • 0
    Thanks @IlmariKaronen - and I bet there are many variants of the first `floor`'s arguments that would achieve the same thing. This is what I was asking though, and its not about "finding an answer that **I like**", per se, it's about finding an answer to the actual question and not getting sidetracked with a different problem altogether!2012-07-06