I compared the loading speed of the source processor between the three main languages (code and results below). I am very curious how the main languages are compared for raw computing power. I had a theory that Java and C # could compete with C ++ when there were no memory related ones.
My questions:
1) Edited (C ++ time is now more realistic)
2) Did I think correctly that the JVM takes age in the first iteration, but for the second it finished the analysis and, therefore, is optimized? How did Hotspot know to complete the optimization after the first iteration of my outer loop, and not halfway?
3) Why does C # not work like Java and is highly optimized from the very beginning? What makes C # different from Java? Why is C # slower - is it just because of less optimization?
4) Is there any specific reason why the fluctuations between 2246 and 2262 milliseconds for C # testing timings can it be two different times, because the processor has two cores?
EDIT: code update to display stopwatch usage in C # code.
EDIT: correct code and C ++ sync results
Setup:
C ++: VS2010 and Intel Compiler (built-in release mode, optimization: O2, Enable internal functions: yes, support size and speed: none, skip frame pointers: No, enable optimization using fibers: no, overall program optimization: Yes)
Java: Eclipse, Hotspot 64-bit compiler version 17, Java 1.6
#: VS2010 .net 4.0 ( )
: Intel E6600 (2,4 ), 2,7 , 300 , 8 , DRAM: 375
- Win 7 (64 )
++:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
#include <fstream>
using namespace std;
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
cout << "QueryPerformanceFrequency failed!\n";
PCFreq = li.QuadPart;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
static long counter = 0;
int _tmain(int argc, _TCHAR* argv[])
{
for (int m = 0; m < 10; m++)
{
StartCounter();
counter = 0;
for (int j = 0; j < 3; j++)
{
for (long i = 0; i < 200000000; i++)
{
counter++;
}
}
cout << GetCounter()*1000000 << " microseconds" << endl;
}
int p = 0;
cin >> p;
return 0;
}
++:
7,19
1,89
2,27
1,51
4,92
10.22
10.22
9,84
9,84
10.6
Java-:
public class main {
static long counter = 0;
public static void main(String[] args) {
for(int m=0; m<10; m++){
long start = System.nanoTime();
counter = 0;
for(int j=0;j<3; j++){
for(long i=0; i<200000000; i++){
counter++;
}
}
System.out.println(((System.nanoTime()-start)/1000000) + " ms");
}
}
}
Java:
5703 milliseconds
471 ms
468 ms
467 ms
469 ms
467 ms
467 ms
467 ms
469 ms
464 ms
#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics
namespace t1
{
class Program
{
static long counter = 0;
static void Main(string[] args)
{
for (int m = 0; m < 10; m++)
{
Stopwatch s = new Stopwatch();
s.Start();
counter = 0;
for (int j = 0; j < 3; j++)
{
for (long i = 0; i < 200000000; i++)
{
counter++;
}
}
s.Stop();
Console.WriteLine(s.Elapsed.TotalMilliseconds + " ms");
}
Console.ReadLine();
}
}
}
#:
2277
2246
2262
2246
2262
2246
2262
2246
2262
2262