云机和主机上运行golang,julia,octave,python,c,java,计算十万,百万,一千万次不同浮点数的正弦值,统计计算耗时
简介:
实验环境
c语言
Python
Java
golang
julia
实验环境:
本机是win10系统,云机是centos7系统
c语言:
在本机上我使用的是notepad++编辑器,编译是在win10窗口下运行的,采用的是tcc编译的。
c代码:
十万次不同浮点数的正弦值代码
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define PI 3.1415
void main(){
int begintime,endtime;
int i = 0;
float n;
begintime=clock(); //开始计时
for(i=0;i<100000;i++)
{
n=(rand()%100000)*0.00001;
printf("%.8f %.8f %.8f\n",n ,sin(n*PI/180),cos(n*PI/180));
}
endtime = clock(); //结束计时
printf("\n\nRunning Time:%dms\n",endtime-begintime);
}
本机运行结果:
0.05801000 0.00101244 0.99999949
0.04280000 0.00074698 0.99999972
0.05253000 0.00091679 0.99999958
0.01629000 0.00028431 0.99999996
Running Time:31168ms
运行时间是31168ms
云机上运行结果:
这个在云机上运行要使用 gcc sin.c -o sin -lm,命令不然编译不出来
0.48302999 0.00843012 0.99996447
0.58275002 0.01017043 0.99994828
Running Time:250000ms
(base) [root@VM_0_17_centos 汇报文档1]#
云运行时间250000ms
一百万次不同浮点数的正弦值代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define PI 3.1415
void main(){
int begintime,endtime;
int i = 0;
float n;
begintime=clock(); //开始计时
for(i=0;i<1000000;i++)
{
n=(rand()%100000)*0.00001;
printf("%.8f %.8f %.8f\n",n ,sin(n*PI/180),cos(n*PI/180));
}
endtime = clock(); //结束计时
printf("\n\nRunning Time:%dms\n",endtime-begintime);
}
本机运行结果:
0.09261000 0.00161630 0.99999869
0.13863000 0.00241948 0.99999707
0.08108000 0.00141507 0.99999900
0.17009000 0.00296854 0.99999559
0.17172000 0.00299699 0.99999551
0.12679000 0.00221284 0.99999755
Running Time:164844ms
F:\tcc\tcc\汇报文档1>
本机运行时间是164844ms,,,和十万次相比,整体运行速度快了
云机运行结果:
647999 0.00761771 0.99997098
0.90952998 0.01587316 0.99987401
0.89455998 0.01561192 0.99987813
0.72215003 0.01260319 0.99992058
0.82911003 0.01446977 0.99989531
0.04087000 0.00071329 0.99999975
0.02526000 0.00044086 0.99999990
0.84438002 0.01473624 0.99989142
0.57853001 0.01009678 0.99994903
Running Time:2520000ms
(base) [root@VM_0_17_centos 汇报文档1]#
云服务器运行时间2520000ms
一千万次不同浮点数的正弦值代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define PI 3.1415
void main(){
int begintime,endtime;
int i = 0;
float n;
begintime=clock();
for(i=0;i<10000000;i++)
{
n=(rand()%100000)*0.00001;
printf("%.8f %.8f %.8f\n",n ,sin(n*PI/180),cos(n*PI/180));
}
endtime = clock();
printf("\n\nRunning Time:%dms\n",endtime-begintime);
}
本机运行结果:
0.22149999 0.00386578 0.99999253
0.13259000 0.00231406 0.99999732
0.27779001 0.00484819 0.99998825
Running Time:1228715ms
F:\tcc\tcc\汇报文档1>
本机运行时间是1228715ms,相对于一百万次又在整体上有所加速
云机运行结果:
0.06584000 0.00114909 0.99999934
0.69124001 0.01206377 0.99992723
0.64973998 0.01133952 0.99993571
0.78039998 0.01361973 0.99990725
0.19524001 0.00340747 0.99999419
0.97907001 0.01708666 0.99985401
0.87031001 0.01518874 0.99988464
Running Time:25350000ms
云机运行时间:25350000ms
python
我使用的是python自带的编辑器idle,编译是在win10窗口下运行的。
十万次不同浮点数的正弦值代码
import numpy as np
import time
start=time.perf_counter()
t =np.arange(0,1,0.00001)
for i in t:
s = np.sin(np.pi*i/180)
c = np.cos(np.pi*i/180)
print(i,s,c)
end=time.process_time()
print('Runing time.%s Seconds'%(end-start))
本机运行结果时间:
0.9999929999999999 0.01745228428284329 0.9998476972885975
0.9999939999999999 0.017452301733477622 0.9998476969839974
0.999995 0.01745231918411195 0.9998476966793972
0.999996 0.017452336634746275 0.9998476963747966
0.9999969999999999 0.017452354085380588 0.9998476960701957
0.9999979999999999 0.017452371536014904 0.9998476957655945
0.999999 0.017452388986649213 0.9998476954609931
Runing time.9.7488590785 Seconds
I:\python代码\汇报文档>
本机运行时间9.7488590785s ,和c语言相比慢好多
云机运行结果时间:
0.9999500000000001 0.017451533905561922 0.9998477103861083
0.9999600000000001 0.017451708411907303 0.9998477073402258
0.9999700000000001 0.017451882918252153 0.9998477042943128
0.9999800000000001 0.01745205742459647 0.9998477012483694
0.99999 0.017452231930940258 0.9998476982023956
Runing time.-15415.782753022 Seconds
(base) [root@VM_0_17_centos 汇报文档]#
云机上运行时间:-15415.782753022 Seconds,云机运行速度明显快于本机
一百万次不同浮点数的正弦值代码
import numpy as np
import time
start=time.perf_counter()
t =np.arange(0,1,0.000001)
for i in t:
s = np.sin(np.pi*i/180)
c = np.cos(np.pi*i/180)
print(i,s,c)
end=time.process_time()
print('Runing time.%s Seconds'%(end-start))
本机运行结果时间:
0.9999992 0.01745239247677607 0.9998476954000727
0.9999992999999999 0.0174523942218395 0.9998476953696125
0.9999994 0.017452395966902932 0.9998476953391524
0.9999994999999999 0.01745239771196636 0.9998476953086922
0.9999996 0.017452399457029793 0.999847695278232
0.9999996999999999 0.01745240120209322 0.9998476952477718
0.9999998 0.017452402947156654 0.9998476952173116
0.9999998999999999 0.01745240469222008 0.9998476951868515
Runing time.67.41105205082 Seconds
I:\python代码\汇报文档>
本机运行时间:67.41105205082s ,相对于本机的10万次速度整体差不多
云机运行结果时间:
0.999999 0.017452388986649213 0.9998476954609931
0.9999990999999999 0.01745239073171264 0.9998476954305329
0.9999992 0.01745239247677607 0.9998476954000727
0.9999992999999999 0.0174523942218395 0.9998476953696125
0.9999994 0.017452395966902932 0.9998476953391524
0.9999994999999999 0.01745239771196636 0.9998476953086922
0.9999996 0.017452399457029793 0.999847695278232
0.9999996999999999 0.01745240120209322 0.9998476952477718
0.9999998 0.017452402947156654 0.9998476952173116
0.9999998999999999 0.01745240469222008 0.9998476951868515
Runing time.-154181.281969009 Seconds
(base) [root@VM_0_17_centos 汇报文档]# ^C
(base) [root@VM_0_17_centos 汇报文档]#
云机上运行时间:-15418.1281969009s,,,
一千万次不同浮点数的正弦值代码
import numpy as np
import time
start=time.perf_counter()
t =np.arange(0,1,0.0000001)
for i in t:
s = np.sin(np.pi*i/180)
c = np.cos(np.pi*i/180)
print(i,s,c)
end=time.process_time()
print('Runing time.%s Seconds'%(end-start))
本机运行结果时间:
0.9999994 0.017452395966902932 0.9998476953391524
0.9999994999999999 0.01745239771196636 0.9998476953086922
0.9999996 0.017452399457029793 0.999847695278232
0.9999996999999999 0.01745240120209322 0.9998476952477718
0.9999998 0.017452402947156654 0.9998476952173116
0.9999998999999999 0.01745240469222008 0.9998476951868515
Runing time.463.665149412 Seconds
D:\python代码\汇报文档>
本机运行时间:463.665149412 Seconds
云机运行结果时间:
0.9999994999999999 0.01745239771196636 0.9998476953086922
0.9999996 0.017452399457029793 0.999847695278232
0.9999996999999999 0.01745240120209322 0.9998476952477718
0.9999998 0.017452402947156654 0.9998476952173116
0.9999998999999999 0.01745240469222008 0.9998476951868515
Runing time.-243078.302655293 Seconds
云机运行时间:-243078.302655293 Seconds
Java
我使用的是editplus3编辑器,编译是在win10窗口下运行的
十万次不同浮点数的正弦值代码
class Hjw
{
public static void main(String[] args)
{
long startTime=System.currentTimeMillis();
double a;
double pi=3.14;
for(float i=0;i<100000;i++){
float f=i/100000;
a=(pi/180);
double b= Math.cos(a*i);
double c= Math.sin(a*i);
// System.out.format("%.8f%n",f);
System.out.format("%.8f %.8f %.8f%n", f, c,b);
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间: "+(endTime - startTime)+"ms");
}
}
本机运行结果时间:
0.99997002 -0.72303687 -0.69080944
0.99997997 -0.73497703 -0.67809200
0.99998999 -0.74669355 -0.66516821
程序运行时间: 75448ms
I:\java代码\汇报文档2>
本机运行时间:75448ms ,相比c慢好多,相比Python快好多
云机运行结果时间:
0.99993998 -0.68591112 -0.72768533
0.99994999 -0.69850018 -0.71560988
0.99996001 -0.71087668 -0.70331667
0.99997002 -0.72303687 -0.69080944
0.99997997 -0.73497703 -0.67809200
0.99998999 -0.74669355 -0.66516821
程序运行时间: 9268ms
(base) [root@VM_0_17_centos 汇报文档2]#
云机运行时间:9268ms
百万次不同浮点数的正弦值代码
class Hjw1
{
public static void main(String[] args)
{
long startTime=System.currentTimeMillis();
double a;
double pi=3.14;
for(float i=0;i<1000000;i++){
float f=i/1000000;
a=(pi/180);
double b= Math.cos(a*i);
double c= Math.sin(a*i);
// System.out.format("%.8f%n",f);
System.out.format("%.8f %.8f %.8f%n", f, c,b);
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间: "+(endTime - startTime)+"ms");
}
}
本机运行结果时间:
0.99999201 0.81868454 -0.57424352
0.99999303 0.80854313 -0.58843692
0.99999398 0.79815567 -0.60245126
0.99999499 0.78752534 -0.61628228
0.99999601 0.77665536 -0.62992575
0.99999702 0.76554904 -0.64337754
0.99999797 0.75420977 -0.65663355
0.99999899 0.74264099 -0.66968975
程序运行时间: 391668ms
I:\java代码\汇报文档2>
本机运行时间:391668ms,和c相比慢太多啊了,python相比快好多
云机运行结果时间:
0.99999100 0.82857683 -0.55987538
0.99999201 0.81868454 -0.57424352
0.99999303 0.80854313 -0.58843692
0.99999398 0.79815567 -0.60245126
0.99999499 0.78752534 -0.61628228
0.99999601 0.77665536 -0.62992575
0.99999702 0.76554904 -0.64337754
0.99999797 0.75420977 -0.65663355
0.99999899 0.74264099 -0.66968975
程序运行时间: 103415ms
(base) [root@VM_0_17_centos 汇报文档2]#
云机运行时间:103415ms
千万次不同浮点数的正弦值代码
class Hjw2
{
public static void main(String[] args)
{
long startTime=System.currentTimeMillis();
double a;
double pi=3.14;
for(float i=0;i<10000000;i++){
float f=i/10000000;
a=(pi/180);
double b= Math.cos(a*i);
double c= Math.sin(a*i);
// System.out.format("%.8f%n",f);
System.out.format("%.8f %.8f %.8f%n", f, c,b);
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间: "+(endTime - startTime)+"ms");
}
}
本机运行结果时间:
0.99999893 -0.86102395 -0.50856440
0.99999899 -0.86976412 -0.49346770
0.99999911 -0.87823962 -0.47822084
0.99999923 -0.88644787 -0.46282845
0.99999928 -0.89438637 -0.44729522
0.99999940 -0.90205271 -0.43162589
0.99999952 -0.90944456 -0.41582520
0.99999958 -0.91655966 -0.39989798
0.99999970 -0.92339584 -0.38384908
0.99999982 -0.92995104 -0.36768336
0.99999988 -0.93622326 -0.35140576
程序运行时间: 2002814ms
I:\java代码\汇报文档2>
本机运行时间:2002814ms
云机运行结果时间:
0.99999893 -0.86102395 -0.50856440
0.99999899 -0.86976412 -0.49346770
0.99999911 -0.87823962 -0.47822084
0.99999923 -0.88644787 -0.46282845
0.99999928 -0.89438637 -0.44729522
0.99999940 -0.90205271 -0.43162589
0.99999952 -0.90944456 -0.41582520
0.99999958 -0.91655966 -0.39989798
0.99999970 -0.92339584 -0.38384908
0.99999982 -0.92995104 -0.36768336
0.99999988 -0.93622326 -0.35140576
程序运行时间: 1576909ms
云机运行时间:1576909ms
golang
我采用的编辑器是中国开发的liteide编辑器,也可以编译运行,这里我就全部在win10命令窗口运行
十万次不同浮点数的正弦值代码
package main //文件所在包main
import (
"fmt"
"math"
"math/rand"
"time"
//引入包fmt
)
func main() { //func是一个关键字,main是一个主函数
start := time.Now()
for i := 0; i < 100000; i++ {
c := rand.Float64()
var pi float64 = 3.1415
b := math.Sin(c * pi / 180)
a := math.Cos(c * pi / 180)
fmt.Println(a, b, c) //调用fmt这个包的一个函数
}
cost := time.Since(start)
fmt.Printf("cost=[%s]", cost)
}
本机运行结果时间:
0.999994833390935 0.003214528182522622 0.18418464725410097
0.9999945974968972 0.0032870924870582663 0.18834242024015024
0.9998795833509061 0.015518337475975993 0.8891971577425763
0.9999194089985363 0.012695491641446928 0.7274390851448634
0.9999905713825652 0.004342481545227153 0.2488139852828219
0.9999478351489565 0.010214058004306184 0.5852498520869169
0.9999144314319856 0.013081659453180629 0.7495673616436112
0.9999535215359796 0.009641305295090791 0.5524309534935193
cost=[14.7315232s]
I:\golong程序\go_code>
本机运行时间:14.7315232s
云机运行结果时间:
0.9999905713825652 0.004342481545227153 0.2488139852828219
0.9999478351489565 0.010214058004306184 0.5852498520869169
0.9999144314319856 0.013081659453180629 0.7495673616436112
0.9999535215359796 0.009641305295090791 0.5524309534935193
cost=[1m31.476300684s](base) [root@VM_0_17_centos 汇报文档3]#
云机运行时间:1m31.476300684s
百万次不同浮点数的正弦值代码
package main //文件所在包main
import (
"fmt"
"math"
"math/rand"
"time"
//引入包fmt
)
func main() { //func是一个关键字,main是一个主函数
start := time.Now()
for i := 0; i < 1000000; i++ {
c := rand.Float64()
var pi float64 = 3.1415
b := math.Sin(c * pi / 180)
a := math.Cos(c * pi / 180)
fmt.Println(a, b, c) //调用fmt这个包的一个函数
}
cost := time.Since(start)
fmt.Printf("cost=[%s]", cost)
}
本机运行结果时间:
0.9999924490061552 0.0038861202595829136 0.22266541695828487
0.9999373144212858 0.011196750776302633 0.6415588901093644
0.9999646985255648 0.008402481935495023 0.48144661653365883
0.9998784742980534 0.015589632304733274 0.8932826652358626
0.9999999997749794 2.121417230956505e-05 0.0012155183880337823
0.9999271682440302 0.012068894210938128 0.6915338849055698
cost=[1m19.1541632s]
I:\golong程序\go_code>
本机运行时间lm19.1541632s
云机运行结果时间:
0.9999646985255648 0.008402481935495023 0.48144661653365883
0.9998784742980534 0.015589632304733274 0.8932826652358626
0.9999999997749794 2.121417230956505e-05 0.0012155183880337823
0.9999271682440302 0.012068894210938128 0.6915338849055698
cost=[14m3.920909476s]
云机运行时间:14m3.920909476s
千万次不同浮点数的正弦值代码
package main //文件所在包main
import (
"fmt"
"math"
"math/rand"
"time"
) //引入包fmt,math,time,math/rand
func main() { //func是一个关键字,main是一个主函数
start := time.Now()
for i := 0; i < 10000000; i++ {
c := rand.Float64()
var pi float64 = 3.1415
b := math.Sin(c * pi / 180)
a := math.Cos(c * pi / 180)
fmt.Println(a, b, c) //调用fmt这个包的一个函数
}
cost := time.Since(start)
fmt.Printf("cost=[%s]", cost)
}
本机运行结果时间:
0.9998998223976465 0.014154333935401484 0.8110345976812745
0.9999838322843707 0.005686402189747993 0.3258182111597408
0.9998589817008006 0.016793353216026163 0.9622618739795078
0.9999988461621413 0.0015191031519480086 0.08704079978310163
0.9998551839929443 0.01701796234675305 0.975133248251753
0.9999997330416314 0.000730696014803921 0.04186703624658029
0.9999975365775767 0.0022196483455909797 0.1271803375083845
0.9999342300914827 0.0114688923333294 0.6571529141078967
cost=[7m41.5104498s]
I:\golong程序\go_code>
本机运行时间:7m41.5104498s
云机运行结果时间:
0.9998589817008006 0.016793353216026163 0.9622618739795078
0.9999988461621413 0.0015191031519480086 0.08704079978310163
0.9998551839929443 0.01701796234675305 0.975133248251753
0.9999997330416314 0.000730696014803921 0.04186703624658029
0.9999975365775767 0.0022196483455909797 0.1271803375083845
0.9999342300914827 0.0114688923333294 0.6571529141078967
cost=[35m18.423696655s]
云机运行时间:35m18.423696655s
octave
本机上我用的是最新版的octave5-1-0版本
十万次不同浮点数的正弦值代码
octave:16> tic;
octave:17> for k=1:100000
> a=rand(1,1);
> sin(a*pi/180);
> cos(a*pi/180);
> end
octave:18> toc;
本机运行结果时间:
>> toc;
Elapsed time is 75.861 seconds.
本机运行时间:75.861 s
云机运行结果时间:
octave:39> toc;
Elapsed time is 45.8987 seconds.
云机运行时间:45.8987seconds
百万次不同浮点数的正弦值代码
octave:16> tic;
octave:17> for k=1:1000000
> a=rand(1,1);
> sin(a*pi/180);
> cos(a*pi/180);
> end
octave:18> toc;
本机运行结果时间:
>> toc;
Elapsed time is 119.613 seconds.
本机运行时间:119.613 s
云机运行结果时间:
octave:30> toc;
Elapsed time is 86.1641 seconds.
云机运行时间:86.1641 seconds.
千万次不同浮点数的正弦值代码
octave:16> tic;
octave:17> for k=1:10000000
> a=rand(1,1);
> sin(a*pi/180);
> cos(a*pi/180);
> end
octave:18> toc;
本机运行结果时间:
᐀>> toc;
Elapsed time is 575.841 seconds.
云机运行结果时间:
octave:33> toc;
Elapsed time is 296.095 seconds.
云机运行时间:296.095 seconds
julia
云机使用的版本是julia1.1.1
十万次不同浮点数的正弦值代码
using Pkg
using PyCall
@pyimport math # 表示调用python 中math库
@pyimport datetime #表示调用时间库
t1 = datetime.datetime.now() #计时开始
for i in 1:100000
a=rand(1)
b=math.sin(math.pi*a/180)
c=math.cos(math.pi*a/180)
println(b,c)
end
t2 = datetime.datetime.now()
println((t2 - t1))
本机运行结果时间
julia> t2 = datetime.datetime.now()
2019-10-28T20:30:22.456
julia> println((t2 - t1))
60125 milliseconds
本机运行时间:60125ms
云机运行结果时间
0.012042535624649922
0.008174816574793015
julia> t2 = datetime.datetime.now()
2019-10-28T14:27:40.169
julia> println((t2 - t1))
80715 milliseconds
云机运行时间:80715ms
百万次不同浮点数的正弦值代码
using Pkg
using PyCall
@pyimport math # 表示调用python 中math库
@pyimport datetime #表示调用时间库
t1 = datetime.datetime.now() #计时开始
for i in 1:1000000
a=rand(1)
b=math.sin(math.pi*a/180)
c=math.cos(math.pi*a/180)
println(b,c)
end
t2 = datetime.datetime.now()
println((t2 - t1))
本机运行结果时间
julia> t2 = datetime.datetime.now()
2019-10-28T20:40:01.983
julia> println((t2 - t1))
287567 milliseconds
julia>
本机运行时间:287567 ms
云机运行结果时间
julia> t2 = datetime.datetime.now()
2019-10-28T14:44:25.904
julia> println((t2 - t1))
346389 milliseconds
julia>
云机运行时间:346389 ms
千万次不同浮点数的正弦值代码
using Pkg
using PyCall
@pyimport math # 表示调用python 中math库
@pyimport datetime #表示调用时间库
t1 = datetime.datetime.now() #计时开始
for i in 1:10000000
a=rand(1)
b=math.sin(math.pi*a/180)
c=math.cos(math.pi*a/180)
println(b,c)
end
t2 = datetime.datetime.now()
println((t2 - t1))
本机运行结果时间
julia> t2 = datetime.datetime.now()
2019-10-28T20:48:52.046
julia> println((t2 - t1))
1305821 milliseconds
本机运行时间:1305821 ms
云机运行结果时间
julia> t2 = datetime.datetime.now()
2019-10-28T15:01:43.405
julia> println((t2 - t1))
1406510 milliseconds
云机运行时间:1406510ms
总结:
本机
次数/语言 | c语言 | python | java | golang | octave | julia |
---|---|---|---|---|---|---|
十万次 | 31168ms | 9.7488590785s | 75448ms | 14.7315232s | 75.861 s | 60125ms |
百万次 | 164844ms | 67.41105205082s | 391668ms | lm19.1541632s | 119.613 s | 287567 ms |
千万次 | 1228715ms | 463.665149412 S | 2002814ms | 7m41.5104498s | 575.841 s | 1305821 ms |
次数/语言 | c语言 | python | Java | golang | octave | julia |
---|---|---|---|---|---|---|
十万次 | 250000ms | -15415.782753022 S | 9268ms | 1m31.476300684s | 45.8987s | 80715ms |
百万次 | 2520000ms | -15418.1281969009s | 103415ms | 14m3.920909476s | 86.1641 s | 346389 ms |
千万次 | 25350000ms | -243078.302655293S | 1576909ms | 35m18.423696655s | 296.095 s | 1406510ms |
具体结论我无法得出,由于电脑配置原因,运行速度有很大误差。各位可以试试