Help - Search - Member List - Calendar
Full Version: Time spent in a loop... not quite linear...
Krazy Letter Forums > Technology > Tech Help > Programming Help
Mynck
Ok, so I was testing my thing to see how fast it was, when I noticed something rather strange...







Number of times loopedTime spent in loop (ms)
20.45
200.5
2000.83
20004.2
20,00035
200,00075
2,000,000500


I would've expected the correlation to be linear, but with this clearly isn't. I could make a graph to better illustrate, but I don't want to. Anyone have an explanation? This is a normal number-processing loop, which I set up so that it's given random input.
The Admiral
What's it processing specifically, and is anything in the loop changing each time through? For instance, Ed's prime number generator for TI 83, which is rechecking all previous prime numbers calculated, will obviously take longer and longer to execute for each additional loop.
Mynck
The body of the loop converts a single pixel of HSL color model into a pixel in the RGB color model. I just fed it random numbers for the test.
The Admiral
What happens if you run your test again, do you get the same results? True, it seems like it should be linear, but on the outside chance...
Mynck
I consistently get similar results. What's really ticking me off here is the relatively high increase between 2,000 and 20,000 iterations, because that makes it pretty hard to have a framerate of 30 fps, or even 20.
Mynck
*bump*

Here's the code, if anyone's interested:
CODE
import java.lang.reflect.Array;

public class Test {
static int h;
static int s;
static int l;
static int m1;
static int m2;
public static void main(String[] args) {
 int num = Integer.parseInt(args[0]);
 int black = Integer.parseInt(args[1]);
 int[] thing = new int[num];
 for (int i = num - 1; i >= 0; i--) {
  thing[i] = (Math.random() * 100 < black) ? 0 : (int)(Math.random() * Math.pow(2, 24));
 }
 System.out.println("Starting...");
 long start = System.nanoTime();
 HSLtoRGB(thing);
 long end = System.nanoTime();
 System.out.println("Finished.");
 System.out.println("Time: " + ((end-start) / 1000000.0) + " ms");
}

static int[] HSLtoRGB(int[] pixels) {
 for (int i = Array.getLength(pixels); --i >= 0;) {
  if (pixels[i] != 0) {  //return black immediately
   h = pixels[i] >> 16;
   s = pixels[i] >> 8 & 0x00FF;
   l = pixels[i] & 0x0000FF;
//    if (h >= 240 || h < 0) throw new RuntimeException("Hue out of bounds");
//    if (s >= 240 || s < 0) throw new RuntimeException("Sat out of bounds");
//    if (l >= 240 || l < 0) throw new RuntimeException("Lum out of bounds");
   m2 = (l <= 120) ? l * (s + 240) : l+s - l*s/240;
   m1 = l*2 - m2;
   pixels[i] = (16+((h < 40 || h >= 200)  ? m2 :
    (h < 80)   ? m1 + 6*(m2-m1)*(180-h) :
    (h < 160)  ? m1 :
  /*(h < 200)*/  m1 + 6*(m2-m1)*h
 )) << 16;

   pixels[i] += (16+((h >= 160) ? m1 :
     (h >= 120) ? m1 + 6*(m2-m1)*(180-h) :
     (h >= 40)  ? m2 :
   /*(h <= 39)*/  m1 + 6*(m2-m1)*h
 )) << 8;

   pixels[i] += 16+((h < 80) ? m1 :
     (h >= 200) ? m1 + 6*(m2-m1)*(180-h) :
     (h >= 120) ? m2 :
   /*(h >= 80)*/  m1 + 6*(m2-m1)*h);
  }
 }
 return pixels;
}

}


Btw, if anyone's interested in helping me make this more efficient or something... don't bother. I already rewrote the entire thing.
The Admiral
I can't read Java but it was an interesting puzzle anyway.
thisoldmage
QUOTE(The Admiral @ Feb 17 2006, 11:27 PM)
What's it processing specifically, and is anything in the loop changing each time through? For instance, Ed's prime number generator for TI 83, which is rechecking all previous prime numbers calculated, will obviously take longer and longer to execute for each additional loop.
*


I'm just wondering, would it be possible to incorporate the Riemann Hypothesis into such a program.

I'm great with math, but I don't program much. Oh yeah, and if you can prove the Riemann Hypothesis there is a prize of 1million dollars
lappy512
Too bad we already chose our topic on graphing in math class eh Admiral?
Spaceman3750
GRAPHING!!! AAAAAAAHHHHHH! *runs out and runs of a cliff in terror*

Meh, not really, it's easy, just annoying and time consuming laugh.gif.
Mynck
What you have to write an essay on graphing?

*shudder*
That'd be horrible.
Spaceman3750
Just plug it into your graphing calculator, I want to see what kind of curve this thing has...
lappy512
Nothing I can see, I wish my caculator can set the scale to a logrithmic scale, would be a lot more helpful...

Edit: Can you make a simple loop, like just $i++ or something, want to see if that would make a difference.
Mynck
0.05
0.3
3.4
31
40
425

These are the differences from one sample data thing to another. It looks like the amount of increase increases approximately linearly with the number of times it's looped through. I know something else with that pattern. Square numbers.

It's just the 31 and the 40 that both seem to take the same space.


-----Edit-----

I also did that thing that lappy512 suggested. The results and the full source code is attached.

Here's the main measuring code thing, though. As you can see in lines 3 and 4, I just used an empty loop.
CODE

static long loop(int i) {
 long start = System.nanoTime();
 for (; i > 0; i--) {
 }
 long end = System.nanoTime();
 return end - start;
}


The data comes out... pretty much linear. Very linear.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.