文章目录
一 在windows系统中
1.1 python
使用python计算sin(3.14),循环不同次数,计算时间:
1.1.1 循环十万次
import math
import time
start = time.time()
for i in range(100000):
y = math.sin(3.14)
end = time.time()
print("耗时{0}".format(end-start))
# print(y)
结果:
1.1.2 循环一百万次
import math
import time
start = time.time()
for i in range(1000000):
y = math.sin(3.14)
end = time.time()
print("耗时{0}".format(end-start))
# print(y)
结果:
1.1.3 循环一千万次
import math
import time
start = time.time()
for i in range(10000000):
y = math.sin(3.14)
end = time.time()
print("耗时{0}".format(end-start))
# print(y)
结果:
1.2 c语言
使用c语言编程计算sin(3.14),循环不同次数,计算时间:
1.2.1 循环十万次
#include <stdio.h>
#include <math.h>
#include<time.h>
clock_t start, end;
double t;
int i;
float y;
int main()
{
start=clock();
for (i=0;i<=100000;i++)
{
y = sin(3.14);
}
end = clock();
t=(double)(end-start)/CLK_TCK;
printf("%f\n",t);
//printf("%lf\n",y);
}
结果:
1.2.2 循环一百万次
#include <stdio.h>
#include <math.h>
#include<time.h>
clock_t start, end;
double t;
int i;
float y;
int main()
{
start=clock();
for (i=0;i<=1000000;i++)
{
y = sin(3.14);
}
end = clock();
t=(double)(end-start)/CLK_TCK;
printf("%f\n",t);
//printf("%lf\n",y);
}
结果:
1.2.3 循环一千万次
#include <stdio.h>
#include <math.h>
#include<time.h>
clock_t start, end;
double t;
int i;
float y;
int main()
{
start=clock();
for (i=0;i<=10000000;i++)
{
y = sin(3.14);
}
end = clock();
t=(double)(end-start)/CLK_TCK;
printf("%f\n",t);
//printf("%lf\n",y);
}
结果:
1.3 java
使用java语言编程计算sin(3.14),循环不同次数,计算时间:
1.3.1 循环十万次
public class Sinx1{
public static void main(String[] args){ //程序入口
double y;
//double degrees = 3.14;
//double radians = Math.toRadians(degrees);
double startTime = System.currentTimeMillis();
for (int i=1; i<=100000;i++){
y = Math.sin(3.14);
//System.out.printn(y);
}
double endTime = System.currentTimeMillis();
double Time = (endTime-startTime)/1000;
System.out.println(Time);
}
}
结果:
1.3.2 循环一百万次
public class Sinx1{
public static void main(String[] args){ //程序入口
double y;
//double degrees = 3.14;
//double radians = Math.toRadians(degrees);
double startTime = System.currentTimeMillis();
for (int i=1; i<=1000000;i++){
y = Math.sin(3.14);
//System.out.printn(y);
}
double endTime = System.currentTimeMillis();
double Time = (endTime-startTime)/1000;
System.out.println(Time);
}
}
结果:
1.3.3 循环一千万次
public class Sinx1{
public static void main(String[] args){ //程序入口
double y;
//double degrees = 3.14;
//double radians = Math.toRadians(degrees);
double startTime = System.currentTimeMillis();
for (int i=1; i<=10000000;i++){
y = Math.sin(3.14);
//System.out.printn(y);
}
double endTime = System.currentTimeMillis();
double Time = (endTime-startTime)/1000;
System.out.println(Time);
}
}
结果:
1.4 octave:
1.4.1 循环十万次
tic
for i=1:1:100000
y = sin(3.14);
endfor
toc
结果:
1.4.2 循环一百万次
tic
for i=1:1:1000000
y = sin(3.14);
endfor
toc
结果:
1.4.3 循环一千万次
tic
for i=1:1:10000000
y = sin(3.14);
endfor
toc
结果:
1.5 golang语言
1.5.1 循环十万次
程序:
package main
import (
"fmt"
"math"
"time"
)
func main() {
start := time.Now()
for i := 0; i < 100000; i++ {
math.Sin(3.14)
}
cost := time.Since(start)
fmt.Printf("%s\n", cost)
}
结果:
1.5.2 循环一百万次
结果:
1.5.3 循环一千万次
结果:
1.6 julia语言
1.6.1 循环十万次
程序:
t1 = time()
for i in 1:100000
sin(3.14)
end
t2 =time()
println((t2-t1)*1)
结果:
单位为秒
1.6.2 循环一百万次
结果:
1.6.3 循环一千万次
结果:
二 在linux系统中
2.1 python
程序同在windows中一样,不过花费时间更少,效率更高。
2.1.1 循环十万次
程序:
import math
import time
start = time.time()
for i in range(100000):
y = math.sin(3.14)
end = time.time()
print("耗时{0}".format(end-start))
# print(y)
结果:
2.1.2 循环一百万次
结果:
2.1.3 循环一千万次
结果:
2.2 c语言
2.2.1 循环十万次
程序:
#include <stdio.h>
#include <math.h>
#include<time.h>
clock_t start, end;
double t;
int i;
float y;
int main()
{
start=clock();
for (i=0;i<=100000;i++)
{
y = sin(3.14);
}
end = clock();
t=(double)(end-start)/1000000.000000; //将时间转换为秒
printf("%.15lfs\n",t);
//printf("%lf\n",y);
}
由于时间很短,所以显示为0s,
结果:
2.2.2 循环一百万次
结果:
2.2.3 循环一千万次
结果:
2.3 java
2.3.1 循环十万次
程序:
public class Sinx3{
public static void main(String[] args){
double y;
//double degrees = 3.14;
//double radians = Math.toRadians(degrees);
double startTime = System.currentTimeMillis();
for (int i=1; i<=100000;i++){
y = Math.sin(3.14);
//System.out.printn(y);
}
double endTime = System.currentTimeMillis();
double Time = (endTime-startTime)/1000;
System.out.println(Time+"s");
}
}
结果:
2.3.2 循环一百万次
结果:
2.3.3 循环一千万次
结果:
2.4 octave
octave安装可以参考我的另外一篇博客:
https://blog.csdn.net/qq_44420246/article/details/102767943
2.4.1 循环十万次
tic
for i=1:1:100000
y = sin(3.14);
endfor
toc
结果:
2.4.2 循环一百万次
tic
for i=1:1:1000000
y = sin(3.14);
endfor
toc
结果:
2.4.3 循环一千万次
tic
for i=1:1:10000000
y = sin(3.14);
endfor
toc
结果:
2.5 golang语言
golang下载安装:
https://www.cnblogs.com/chnmig/p/10107158.html
2.5.1 循环十万次
程序:
package main
import (
"fmt"
"math"
"time"
)
func main() {
start := time.Now()
for i := 0; i < 100000; i++ {
math.Sin(3.14)
}
cost := time.Since(start)
fmt.Printf("%s\n", cost)
}
结果:
2.5.2 循环一百万次
结果:
2.5.3 循环一千万次
结果:
2.6 Julia语言
julia在linux系统上的安装和配置参考了如下两个博客:
安装:
https://blog.csdn.net/kingyuan666/article/details/81592822
报错配置:
https://blog.csdn.net/taw19960426/article/details/102754209
2.6.1 循环十万次
程序:
t1 = time()
for i in 1:100000
sin(2*pi*500*i)
end
t2 =time()
println((t2-t1)*1)
结果:
单位为秒。
2.6.2 循环一百万次
结果:
2.6.3 循环一千万次
结果:
三 总结
从上面的结果来看,在linux系统中运行效率明显高于windows系统,但对于golang语言和julia语言在windows和linux上则不相上下。
在windows系统中运行时间比较
python循环一千万次sin(3.14)的时间约为:
3.972s
c语言循环一千万次sin(3.14)的时间为:
0.532s
java循环一千万次sin(3.14)的时间为:
0.209s
octave循环一千万次sin(3.14)的时间为:
92s
golang循环一千万次sin(3.14)的时间约为:
0.214s
julia循环一千万次sin(3.14)的时间约为:
0.103s
所以运行效率:
julia>java>golang>c>python>octave
在linux系统中运行时间比较
python循环一千万次sin(3.14)的时间约为:
1.69s
c语言循环一千万次sin(3.14)的时间为:
0.02s
java循环一千万次sin(3.14)的时间为:
0.147s
octave循环一千万次sin(3.14)的时间为:
24.5216s
golang循环一千万次sin(3.14)的时间约为:
0.114s
julia循环一千万次sin(3.14)的时间约为:
0.127s
所以运行效率:
c>golang>julia>java>python>octave
这几门语言中效率最低的为octave。