On Oct 13, 2006, at 4:46 AM, Ronald Vogelaar wrote:

As it now appears, my tests were flawed.

Here's the code:

 #pragma disableBackgroundTasks
 #pragma disableautoWaitCursor
 #pragma disableBoundsChecking
 #pragma nilObjectChecking false

 dim time, time2 as Double
 dim i, j as Integer

 time=microseconds

 For j=0 to 100000000
   i=i+1
 Next

 time=microseconds-time
 i=0
 time2=microseconds

 For j=0 to 100000000
   i=i--1
 Next

 time2 =microseconds-time2
 msgbox str( time )+chrb(10)+str( time2 )

As it turns out, the second loop is always faster than the first. I cannot explain it, but when I switched the two loops the earlier results were reversed. Perhaps the instructions are cached by the CPU?

Whenever you want to test a very short task, you must unroll it in the loop yourself. In the above tests, you have the loop in the calculation as well. For example, the code actually would look like this:

j = 0
loopStart:
if j < 10000000 then
  i = i - -1
  j = j + 1
  goto loopStart
end if

As you can see, the item we're trying to test is only 1 statement out of 5. Instead, you should try this loop to benchmark such a short operation:

 For j=0 to 100000000
   i=i--1
   i=i--1
   i=i--1
   i=i--1
...

By unrolling the loop, you ensure that the overhead of the loop barely affects the results. This should bring the results into the expected range. The other thing to do is to run the tests multiple times. The OS will often cycle the CPU when it's not being used, which can cause some "startup" expense when it realizes it's actually being used for something lengthy. By separating the tests into their own methods and calling them individually, you can eliminate this aspect of things as well.

Just FYI, the "i = i - -1" will be eventually compiled into a IA32 opcode "sub" with an immediate value since we're subtracting a constant. "i = i + 1" will eventually be compiled into the "add" opcode with an immediate value also. I'm guessing that these operations should be the exact same number of CPU cycles because they are such similar and related functionality, and Intel/AMD would get a lot of grief if they had differing performances for add and sub :)

HTH,
Jon


--
Jonathan Johnson
[EMAIL PROTECTED]
REAL Software, Inc.


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to