Re: The inaugural VB6 vs dot net test



Mike Williams wrote:

"Ulrich Korndoerfer" <ulrich_wants_nospam@xxxxxxxxxxxx> wrote in message news:OY10nF8MIHA.5040@xxxxxxxxxxxxxxxxxxxxxxx

. . . and changed the loop direction in Invert1 back to
counting from 0 up. Counting down actually is slower.

Yep. That's why I counted up in my own code. In this particular case counting down means you are addressing the bitmap memory in a "top down" fashion as well, which kind of "goes against the grain" of the memory cache, hence slowing the code down.

In my oppinion the reason why one could hope that counting down to 0 makes loops running faster comes from how the compiler handles those loops. Some time ago I had a look at the assembler code the compiler generates for counting up loops. As far as I remember, the final value (the loop stops when the loop counter exceeds this value) is stored in a local variable on the stack. On each iteration this final value is loaded back from the stack into a CPU register and then compared with the loop counter. If however a loop counts down, the final value is zero, and so the compiler theoretically could optimize this by just comparing against zero. It would not be necessary to load the final value for each iteration, so the loop should execute a litle bit faster. I hadn't a look on the assembler code generated for this case, but as the counting down loop is not faster, I assume the compiler still loads a value from the stack (being zero), not doing the optimization it could do here.

Now both methods are equally fast. Invert3 respects padding
bits, but is optimized for 24bpp, Invert1 is more general
regarding the bpp, but changes padding bits.
// New test results
Compiled to native code, settings are:
"Compile for speed, use *all* extended optimizations"
Intel Core 2 Duo @ 1.86 GHz
1024 x 768 bitmap, 24 bits per pixel
1001 iterations per run, 3 runs
Invert1: 1.504, 1.508, 1.505 sec
Invert3: 1.503, 1.502, 1.504 sec

I'm getting 5.512, 5.461, 5.495 on my own AMD Athlon 3000+ (2.2 Ghz) machine, which just a bit faster than the 6.133, 6.156, 6.166 that I'm getting with my own code. There isn't much of a difference, and I expect it might be because all my code is in the Form whereas your main code is in a module, which might make a bit of difference. Might try using a module for my own code later to see if it makes a difference. Not that it really matters of course, because the speeds are pretty much the same anyway.


Which code did you use? The one you posted (using a two dimensional array and two nested loops) or a version that uses a 'flat' (1 dim) array? Using a two dimensional array implies for each access more adress calculating work: two multiplications (multiplying the column index with the element length and multiplying the row index with the row length) and two additions. Where a 1 dimensional array only needs one multiplication and one addition.

What /is/ interesting though is how much faster your Core 2 Duo 1.86 Ghz runs. I've been putting off getting a new machine for a long time now, thinking that they are not yet fast enough to warrant the change, but having seen your figures I think I'm going to get one pretty soon :-) If one core runs so much faster than my own machine then it should fairly fly when running stuff optimized for two cores. Or maybe I'll wait for the 256 core machines to appear ;-)

Since using the 'core' or 'core2' architecture, Intel processors seem to be faster than the AMD ones. The older architectures Intel used however were slower than the AMD ones.

--
Ulrich Korndoerfer

VB tips, helpers, solutions -> http://www.proSource.de/Downloads/
.



Relevant Pages

  • Re: The inaugural VB6 vs dot net test
    ... increment a third variable inside the inner loop. ... But I was repsonding to Ulrich's statement about counting down to zero as opposed to counting up to a specific value in order to save a couple of clock cycles. ... WTF is the point of doing that if you are then going to include and additional otherwise unnecessary pointer that gets incremented within the loop, which will eat up as least as many clock cycles as you saved, thereby negating your efforts! ...
    (microsoft.public.vb.general.discussion)
  • Re: Tims experiment with carb extremes
    ... Maybe important to lots of people here, but I have no working feedback ... loop in me at all, so I scratch my head about it a lot. ... Maybe I have to do protein counting in addition to carb ... Oops, just googled, the ADA exchange diet is now more than 50 years ...
    (alt.support.diabetes)
  • Re: The inaugural VB6 vs dot net test
    ... ago I had a look at the assembler code the compiler generates for counting ... Well all compilers are different of course, but when I used to write machine code many years ago counting down to zero was definitely faster than counting up to a specific number, because as long a the initial value of the loop counter was such that it contained a number that would be considered positive when viewed as a signed value then you could simply perform a DEC and follow that by a BPL, which would cause the branch to be taken unless the resultant value was negative. ... However, and this is the crucial point, saving a few clock cycles on the loop maintenance instructions is only worthwhile if the act of doing so does not in itself cause the code to run more slowly, as would be the case if you ended up actually reading and writing the memory in a "top down" order, which goes against the grain of the memory cache on many systems and which slows down the code by a much greater margin than it was speeded up by the lesser number of loop maintenance clock cycles. ...
    (microsoft.public.vb.general.discussion)
  • Re: for loop problem
    ... > No, the optimizer reverses loops counting up, but not if the app relies on ... AFAIK, loops counting down ... compiler chooses to implement the loop. ...
    (alt.comp.lang.borland-delphi)
  • Re: Counting help
    ... rooms....Then im trying to list the room once and then count how many records match for the select statement and only print the building once with a count in front of it.. ... time through the loop, that should fix your immediate problem. ... broken the DBI steps into prepare/execute. ... but if counting is your only goal and you have no other reason to select ...
    (perl.beginners)

Loading