Python可以比C++更快,你不信?
from numba import jit
import random
@jit(nopython=True)
def monte_carlo_pi(nsamples):
acc = 0
for i in range(nsamples):
x = random.random()
y = random.random()
if (x ** 2 + y ** 2) < 1.0:
acc += 1
return 4.0 * acc / nsamples
@numba.jit(nopython=True, parallel=True)
def logistic_regression(Y, X, w, iterations):
for i in range(iterations):
w -= np.dot(((1.0 /
(1.0 + np.exp(-Y * np.dot(X, w)))
- 1.0) * Y), X)
return w
import math
import time
def is_prime(num):
if num == 2:
return True
if num <= 1 or not num % 2:
return False
for div in range(3, int(math.sqrt(num) + 1), 2):
if not num % div:
return False
return True
def run_program(N):
total = 0
for i in range(N):
if is_prime(i):
total += 1
return total
if __name__ == "__main__":
N = 10000000
start = time.time()
total = run_program(N)
end = time.time()
print(f"total prime num is {total}")
print(f"cost {end - start}s")
total prime num is 664579
cost 47.386465072631836s
#include <iostream>
#include <cmath>
#include <time.h>
using namespace std;
bool isPrime(int num) {
if (num == 2) return true;
if (num <= 1 || num % 2 == 0) return false;
double sqrt_num = sqrt(double(num));
for (int div = 3; div <= sqrt_num; div +=2){
if (num % div == 0) return false;
}
return true;
}
int run_program(int N){
int total = 0;
for (int i; i < N; i++) {
if(isPrime(i)) total ++;
}
return total;
}
int main()
{
int N = 10000000;
clock_t start,end;
start = clock();
int total = run_program(N);
end = clock();
cout << "total prime num is " << total;
cout << "\ncost " << (end - start) / ((double) CLOCKS_PER_SEC) << "s\n";
return 0;
}
$ g++ isPrime.cpp -o isPrime
$ ./isPrime
total prime num is 664579
cost 2.36221s
import math
import time
from numba import njit
# @njit 相当于 @jit(nopython=True)
@njit
def is_prime(num):
if num == 2:
return True
if num <= 1 or not num % 2:
return False
for div in range(3, int(math.sqrt(num) + 1), 2):
if not num % div:
return False
return True
@njit
def run_program(N):
total = 0
for i in range(N):
if is_prime(i):
total += 1
return total
if __name__ == "__main__":
N = 10000000
start = time.time()
total = run_program(N)
end = time.time()
print(f"total prime num is {total}")
print(f"cost {end - start}s")
total prime num is 664579
cost 3.0948808193206787s
import math
import time
from numba import njit, prange
@njit
def is_prime(num):
if num == 2:
return True
if num <= 1 or not num % 2:
return False
for div in range(3, int(math.sqrt(num) + 1), 2):
if not num % div:
return False
return True
@njit(parallel = True)
def run_program(N):
total = 0
for i in prange(N):
if is_prime(i):
total += 1
return total
if __name__ == "__main__":
N = 10000000
start = time.time()
total = run_program(N)
end = time.time()
print(f"total prime num is {total}")
print(f"cost {end - start}s")
python isPrime.py
total prime num is 664579
cost 1.4398791790008545s
最后的话
推荐阅读:
入门: 最全的零基础学Python的问题 | 零基础学了8个月的Python | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 | 从万众期待到口碑扑街!唐探3令人失望 | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |
趣味:弹球游戏 | 九宫格 | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
年度爆款文案
点阅读原文,领廖雪峰大数据视频资料!
评论