小编Mat*_*usz的帖子

为什么我的python numpy代码比c ++快?

有人可以告诉我为什么这个Python Numpy代码:

import numpy as np
import time

k_max = 40000
N = 10000

data = np.zeros((2,N))
coefs = np.zeros((k_max,2),dtype=float)

t1 = time.time()
for k in xrange(1,k_max+1):
    cos_k = np.cos(k*data[0,:])
    sin_k = np.sin(k*data[0,:])
    coefs[k-1,0] = (data[1,-1]-data[1,0]) + np.sum(data[1,:-1]*(cos_k[:-1] - cos_k[1:]))
    coefs[k-1,1] = np.sum(data[1,:-1]*(sin_k[:-1] - sin_k[1:]))
t2 = time.time()

print('Time:')
print(t2-t1)
Run Code Online (Sandbox Code Playgroud)

比这个C++代码更快:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <time.h>

using namespace std;

// consts
const unsigned int k_max = 40000;
const unsigned int N = 10000;

int main()
{
    time_t …
Run Code Online (Sandbox Code Playgroud)

c++ python numpy

1
推荐指数
3
解决办法
3336
查看次数

标签 统计

c++ ×1

numpy ×1

python ×1