用一個簡單的 numpy 小程式,和一個 C 小程式,執行基本的陣列運算,來測試 numpy 和 C 的執行速度。
- numpy 程式
#! /usr/bin/env python # -*- coding: UTF-8 -*- import sys import time import codecs from Numeric import * if __name__ == '__main__': loopnum = 10000000 sys.stdout.write( "Constructing numpy double array (length %d)...\n" % loopnum ) now = time.time() a = arange(loopnum).astype(Float64) sys.stdout.write( " for %f secs.\n" % (time.time() - now) ) sys.stdout.write( "python double array calculating...\n" ) now = time.time() b = a + a sys.stdout.write( " for %f secs.\n" % (time.time() - now) )
- C 程式 (請使用 gcc -c test test.c -lrt,進行編譯,以確保 clock_gettime() 會被連結進可執行檔)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int i; int loopnum = 10000000; double j; double *a; struct timespec *now; now = (struct timespec *)malloc(2*sizeof(struct timespec)); printf("Constructing C double array (length %d)...\n", loopnum); clock_gettime(CLOCK_REALTIME, now); a = (double *)malloc(loopnum*sizeof(double)); for (i=0; i<loopnum; i++){ a[i] = 0.0; } clock_gettime(CLOCK_REALTIME, now+1); printf( " for %f secs.\n", ( (now+1)->tv_sec - now->tv_sec ) + ( (now+1)->tv_nsec - now->tv_nsec ) * 1.e-9); printf("C double array calculating...\n"); clock_gettime(CLOCK_REALTIME, now); for (i=0; i<loopnum; i++){ j = a[i] + a[i]; } clock_gettime(CLOCK_REALTIME, now+1); printf(" for %f secs.\n", ( (now+1)->tv_sec - now->tv_sec ) + ( (now+1)->tv_nsec - now->tv_nsec ) * 1.e-9); }
在 Xeon 550 上的執行結果是:
- numpy 程式
Constructing numpy double array (length 10000000)... for 3.299104 secs. python double array calculating... for 2.623237 secs.
- C 程式
Constructing C double array (length 10000000)... for 0.576618 secs. C double array calculating... for 0.741613 secs.
numpy 的計算速度比 C 慢了大約 3.5 倍;陣列的建立速度則慢了大約 5.7 倍。
根據這個結果,如果想用 Python 開發計算程式,實用上一定要為計算核心撰寫 C module,否則其速度將是不可接受的。
Navigate
- Previous: げんしけん 第三話 地域文化振興の問題点とその功績 @2004/11/01
- Next: 副檔名測驗 @2004/11/03
Add a trackback
Please send trackback to: http://blog.seety.org/everydaywork/2004/11/2/62/trackback/.
Comments
Add a comment
此时速度差异不大。