Python 5 times faster than C, just simple testing.
Incredible! Without Numba, Python is 30 times slower than C language, but with Numba and fastmath=True enabled, it's 5 times faster than C language - how is this possible?
t1.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t start, finish;
double total_time;
total_time = (double)(finish - start) / CLOCKS_PER_SEC ;
int tt=10000*10000;
printf("Calculate x*x-(x-2)*(x+1) for 100,000,000 times:\n");
start = clock();
for (int x=0;x<tt;x++){
int k=x*x-(x-2)*(x+1);
}
finish = clock();
total_time = (double)(finish - start) / CLOCKS_PER_SEC ;
printf(" In C language: %lf secs,%lf M/sec\n", total_time,tt/total_time/1000/1000);
system("python test.py");
return 0;
}
test py:
import time
import numba
ts=10000*10000
@numba.jit(nopython=True)
def test():
for x in range(ts):
v=x*x-(x-2)*(x+1)
@numba.jit(nopython=True,fastmath=True)
def test1():
for x in range(ts):
v=x*x-(x-2)*(x+1)
def test2():
for x in range(ts):
v=x*x-(x-2)*(x+1)
t0=time.time()
test()
t=time.time()-t0
print("In python with Numba, nopython:%f secs,%f M/sec\n" %(t,ts/t/1000/1000))
t0=time.time()
test1()
t=time.time()-t0
print("In python with Numba, nopython,fastmath:%f secs,%f M/sec\n" %(t,ts/t/1000/1000))
t0=time.time()
test2()
t=time.time()-t0
print("In python without Numba:,%ff secs,%f M/sec\n" %(t,ts/t/1000/1000))
沒有留言:
發佈留言