C# beats C++ STL in simple benchmarking

  • Thread starter Thread starter stork
  • Start date Start date
Too bad you:
- Didn't list compiler settings (Visual C++ settings are a lot more
complicated than optimization on or off).
- Claimed your C++ code is optimal when it isn't.
- Used buggy logic (no protection against pointer wraparound in the C++
pointer code -- this is equivalent to bounds checking but way more
efficient).
- Didn't use optimal C# code either (moving the call to the Length property
outside the loop actually works against you).
- Didn't indicate what methods you used to measure execution time.
- Didn't compare 32-bit and 64-bit CLR.

etc., etc.

You might get better results (e.g. fewer instructions, smaller code size,
faster execution) with:

void populateWithPointerLoop(const int * const arr, int length)
{
int *endarr = arr + length;
if (endarr <= arr) return;
*arr = 0;
--length;
do {
*(--endarr) = length * length;
} while (--length);
}

For the C++ Standard Library (which replaced STL) version, try for_each. It
will eliminate the bounds checking, and the function call should get inlined
anyway.

Of course, if optimizations were really turned up you'd get loop unrolling
and use of SIMD instructions (you mentioned SSE, but didn't use it) which
will be quite a lot faster than the assembler code you posted. You may need
GCC or Intel ICC to get those transformations done automatically.

Another thing is that modern CPUs have way too much complication (branch
prediction, pipelining, etc) to be able to make any pronouncements based
simply on the number of instructions.

Post a complete VS2008 solution and I'll be happy to run it on a Lynnfield
platform (Windows 7 x64, Core i5 quad core, 4 GB RAM).

Not that I disagree with your conclusion that there are more things to
consider when choosing a language than raw speed.
 
Back
Top