First of all, this is not testing the quality of the various languages, at best, and the compilation of the degree of optimization.
C Language:
#include <stdio.h>
#include <time.h>
int main (void) {
Long a=0;
clock_t start, stop;
Double duration;
start = Clock ();
for (int i=0;i<1000000000;i++) {
a++;
}
stop = Clock ();
Duration = (double) (Stop-start))/clocks_per_sec;
printf ("%f", duration);
return 0;
}
The result is 2.9s.
C++
#include <iostream>
#include <time.h>
using namespace Std;
int main () {
Long a=0;
clock_t start, ends;
Double duration;
start = Clock ();
for (int i=0;i<1000000000;i++) {
a++;
}
ends = Clock ();
cout << "Running Time:" << (Double) (ends-start)/clocks_per_sec << Endl;
return 0;
}
Result 3.0s
PHP7:
<?php
$t 1 = microtime (true);
for ($i =0; $i <1000000000; $i + +) {
}
$t 2 = Microtime (true);
Echo ' time-consuming '. Round ($t 2-$t 1,3). ' Seconds ';
?>
The result is 11s.
Java 1.8:
Import java.io.*;
Import java.lang.*;
Import java.util.*;
Class Test
{
public static void Main (string[] args) throws Java.lang.Exception
{
Long start= system.currenttimemillis ();
Long a=0;
for (long i=0;i<1000000000;i++) {
a++;
}
Long end= system.currenttimemillis ();
System.out.println (a);
System.out.println (End-start);
}
}
The result is 0.449s.
Nodejs
var start = new Date (). GetTime ();
var a=0;
for (Var i=0;i<1000000000;i++) {
a++;
}
var end = new Date (). GetTime ();
Console.log ((End-start) + "MS");
The result is 1.17s.
C#:
Using System;
Using System.Diagnostics;
public class Test
{
public static void Main ()
{
Stopwatch SW = new Stopwatch ();
Long a=0;
Sw. Start ();
for (long i=0;i<1000000000;i++) {
a++;
}
Sw. Stop ();
TimeSpan TS2 = sw. Elapsed;
Console.WriteLine ("Stopwatch total cost {0}ms.", Ts2. TotalMilliseconds);
}
}
Result 0.541s
Groovy:
Using System;
Using System.Diagnostics;
public class Test
{
public static void Main ()
{
Stopwatch SW = new Stopwatch ();
Long a=0;
Sw. Start ();
for (long i=0;i<1000000000;i++) {
a++;
}
Sw. Stop ();
TimeSpan TS2 = sw. Elapsed;
Console.WriteLine ("Stopwatch total cost {0}ms.", Ts2. TotalMilliseconds);
}
}
Results: 6.2s
Python3:
#!/usr/bin/env python
# _*_ Coding:utf-8 _*_
Import time
# from Numba import JIT
#
#
# @jit
def test ():
A=0
Start=time.time ()
While a < 1000000000:
A=a+1
End=time.time ()
Print (End-start)
Print (a)
If __name__== "__main__":
Test ()
Result 90s
0.035s
Several languages are cycled 1 billion times