Subject of debate: Which language is faster?
I gave this some thought and decided that rather than presenting source code for some esoteric thing that would slant things in C's favor but would never happen in the real world, I'd like to see the results for something real... sorting a large array of ints.
itsme86... Play fair, no language specific optimizations... just translations...
The baseline is this code in C on a given machine, other languages should compare to that.
On my "I don't care if I fry the CPU" system (AMD64 X2 @2.4ghz, ddr2 800mhz) when compiled in PellesC with full optimizations, this code averages 1030ms for 10 tries... so, just over 10 seconds. Of course the only fair comparison is on the same machine... so...
Code:// create and sort large array
#include <stdio.h>
#include <windows.h>
#define MAXARRAY 100000
int main( void )
{ int x; // loop counter
int y; // loop counter
int max; // maximum value
int swp; // swap pointer
int Swaps = 0; // swap counter
unsigned int Start; // timing
unsigned int Finish;
printf( "Benchmark on %d elements\n",MAXARRAY);
// timestamp
Start = GetTickCount(); // in milliseconds
// create array
int *Array = malloc(MAXARRAY * sizeof(int));
// fill array
srand(Start);
for (x = 0; x < MAXARRAY; x++)
Array[x] = rand() % MAXARRAY;
// sort
for (x = MAXARRAY - 1; x > 0; x--)
{ max = Array[x];
swp = x;
for (y = 0; y < x; y++)
{ if (Array[y] > max)
{ max = Array[y];
swp = y; } }
if (swp < x )
{ Array[swp] = Array[x];
Array[x] = max;
++Swaps; } }
// timestamp
Finish = GetTickCount();
// report
printf("Time = %d milliseconds for %d swaps\n",Finish - Start,Swaps);
return 0; }