用一個簡單的 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,否則其速度將是不可接受的。

Posted by yungyuc at 20:53, 2 comments, 0 trackback.
Add a trackback
Comments
Re: 在陣列的處理速度上,numpy 遠比 C 慢
这个比拼不公平,Python应该用a+=a这样才对。
此时速度差异不大。
BloodMage at 2007-08-25 14:41.
Re: 在陣列的處理速度上,numpy 遠比 C 慢
您說的極是,後來我也注意到了這個問題,所以儘量用 atomic operation。
yungyuc at 2007-08-25 21:12.
Add a comment

Your name. (required)

Your personal website. (optional)

Your email address. Will not show in page. (suggested, but optional)

Text format is "Plain Text".

Enter "ybtki"
© hover year to navigate month: powered by django