这里我用的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:\Users\mh\Desktop>python cal.py
7.873001337051392
C:\Users\mh\Desktop>python cal.py
17.40298104286194
PHP
C:\Users\mh\Desktop>php cal.php
5
C:\Users\mh\Desktop>php cal.php
13
PHP 7.1(新下载的)
C:\Users\mh\Desktop>php cal.php
5
C:\Users\mh\Desktop>php cal.php
10
nodeJS
C:\Users\mh\Desktop>node cal.js
1.288
C:\Users\mh\Desktop>node cal.js
2.603
Golang
C:\Users\mh\Desktop>go run cal.go
0.0519903
C:\Users\mh\Desktop>go run cal.go
0.1080017
C#
0.685999155044556
1.52999567985535
lua(just for fun)
C:\Users\mh\Desktop>cal.lua
1
C:\Users\mh\Desktop>cal.lua
2
PHP7还是比python3.6快的,不过在脚本语言中node更快,Go挺快的
原文地址:https://www.cnblogs.com/zgblog/p/10346176.html
时间: 2024-10-03 15:48:18