Delphi is compiled into native code, while C # is compiled into CLR code, which is then translated at runtime. However, C # uses JIT compilation, so you can expect the time to be more similar, but this is not set.
It would be helpful if you could describe the hardware on which you ran it (CPU, clock speed).
I do not have access to Delphi to repeat your experiment, but using native C ++ vs C # and the following code:
VC ++ 2008
#include <iostream> #include <windows.h> int main(void) { int tick = GetTickCount() ; for (int i = 0; i < 1000000000; ++i) { } tick = GetTickCount() - tick; std::cout << tick << " ms" << std::endl ; }
FROM#
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int tick = System.Environment.TickCount; for (int i = 0; i < 1000000000; ++i) { } tick = System.Environment.TickCount - tick; Console.Write( tick.ToString() + " ms" ) ; } } }
I originally received:
C++ 2792ms C
However, I then rebuilt the C # version and executed the executable in <project>\bin\release and <project>\bin\debug respectively directly from the command line. It gave:
C# (release): 720ms C
So, I believe that this is exactly the difference: you ran the debug version of C # code from the IDE.
If you think C ++ is then especially slow, I ran this as an optimized version of the assembly and got:
C++ (Optimised): 0ms
This is not surprising because the loop is empty and the control variable is not used outside the loop, so the optimizer completely removes it. To avoid this, I declared i as volatile with the following result:
C++ (volatile i): 2932ms
I assume that the C # implementation also deleted the loop and that 720ms is something else; this can explain most of the difference between the timings in the first test.
What Delphi does, I cannot say, you can look at the generated assembly code to see.
All of the above tests on AMD Athlon Dual Core 5000B 2.60GHz, on Windows 7 32bit.