各大语言性能对比PK数据

Posted zgblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各大语言性能对比PK数据相关的知识,希望对你有一定的参考价值。

这里我用的python版本是Python 3.6.2(64位),php版本是PHP 7.0.12(64位),node版本是v6.11.0(64位),Go 1.8.3(64位),C# 基于.Net 4.0,lua 5.1.4

对比

python

import time

calNum = 100000000

def test():
    sum = 0
    for x in range(1, calNum + 1):
        sum += x
    return sum


start = time.time()

test()

end = time.time()

print(end - start)

PHP

<?php

$calNum = 200000000;

function test() {
    $sum = 0;
    global $calNum;
    for ($i = 0; $i < $calNum; $i++) { 
        $sum += $i;
    }
    return $sum;
}

$start = time();

test();

$end = time();

echo ($end - $start);

nodeJS


let calNum = 200000000;

function test() {
    let sum = 0;
    for (var i = 0; i < calNum; i++) {
        sum += i;
    }
    return sum;
}

let start =  new Date().getTime();

test();

let end =  new Date().getTime();

console.log((end - start) / 1000);

Golang

package main

import (
    "fmt"
    "math"
    "time"
)

var calNum = 200000000

func test() int {
    sum := 0
    for i := 0; i < calNum; i++ {
        sum += i
    }
    return sum
}

func main() {
    start := time.Now().UnixNano()
    test()
    end := time.Now().UnixNano()
    fmt.Println(float64(end - start) / math.Pow(10, 9))
}

C#

 private static int calNum = 100000000;

        static int test()
        {
            int sum = 0;
            for (int i = 0; i < calNum; i++)
            {
                sum += i;
            }
            return sum;
        }

        static void Main(string[] args)
        {
            double start = ConvertDateTimeInt(DateTime.Now);
            test();
            double end = ConvertDateTimeInt(DateTime.Now);
            Console.WriteLine(end - start);
            Console.ReadLine();
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static double ConvertDateTimeInt(DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (time - startTime).TotalSeconds;
        }

lua

 
calNum = 200000000


function test()
    local sum = 0
    for i=0,calNum - 1 do
        sum = i + sum
    end
    return sum
end

local start = os.time()
test()
local endTime = os.time()

print(endTime - start)

执行时间(1亿次,2亿次)

python

C:UsersmhDesktop>python cal.py
7.873001337051392

C:UsersmhDesktop>python cal.py
17.40298104286194

PHP

C:UsersmhDesktop>php cal.php
5
C:UsersmhDesktop>php cal.php
13

PHP 7.1(新下载的)

C:UsersmhDesktop>php cal.php
5
C:UsersmhDesktop>php cal.php
10

nodeJS

C:UsersmhDesktop>node cal.js
1.288

C:UsersmhDesktop>node cal.js
2.603

Golang

C:UsersmhDesktop>go run cal.go
0.0519903

C:UsersmhDesktop>go run cal.go
0.1080017

C#

0.685999155044556

1.52999567985535

lua(just for fun)

C:UsersmhDesktop>cal.lua
1

C:UsersmhDesktop>cal.lua
2

PHP7还是比python3.6快的,不过在脚本语言中node更快,Go挺快的

以上是关于各大语言性能对比PK数据的主要内容,如果未能解决你的问题,请参考以下文章

各大主流编程语言性能PK,结果出乎意料

C/C++ PK 各大编程语言

数据库与elasticSearch,大数据查询性能PK

各大加密算法对比(原理性能安全运用)

将基因组数据分类并写出文件,python,awk,R data.table速度PK

Nodejs pk Java 第1回合:初次交锋