Several languages are cycled 1 billion times

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.