内置sort是C写的,这就是C的魅力所在,性能好!
各大语言性能对比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))
}
#
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
#
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挺快的!
我这里整理学习近百本计算机经典书籍,包括各种编程语言,算法,网络编程,数据库,分布式等等各种技术。对于学习计算机的同学帮助非常大,且十分系统!面试找工作的资料汇总都打包放在这了,这套资源可不是一般那种网上找的资源,非常宝贵,这些书能帮助你收割BAT offer,不要错过!
书单:
觉得不错的话,记得帮我 @盼盼编程 点个赞哦,祝大家都能学有所获!
也可以关注下我哟,致力于分享硬核学习路线,技术。希望能帮助更多CS学习者,让他们少走弯路!