Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84499
  • 博文数量: 7
  • 博客积分: 1445
  • 博客等级: 上尉
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-12 15:18
文章分类

全部博文(7)

文章存档

2010年(1)

2008年(6)

我的朋友

分类:

2008-09-07 15:18:33

我Faint,CU的博客强迫我分页,说编辑数已达上限,接着继续编译我们的testTime.cpp:
lsj@debian007:~/testTime$ g++ testTime.cpp -o testTime
修改我们的testTime.py
lsj@debian007:~/testTime$ chmod +x testTime.py
在10亿次运算的压力下,两者性能差距还是蛮大的:
testTime
lsj@debian007:~/testTime$ ./testTime
for-loop:       5
add:    4
subtract:       4
multiply:       5
divide: 8
divide2:        4
var:    4
if:     5
readGlobal:     3
concatStrings:  279
testTime.py
lsj@debian007:~/testTime$ ./testTime.py
for-loop:       187.0
add:    333.0
substract:      302.0
multiply:       372.0
divide: 341.0
divide2:        342.0
var:    332.0
if:     294.0
readGlobal:     262.0
concatStrings:  421.0
看得我都想提升下pyhotn的性能呢,用c语言来扩充下,看看效果:
lsj@debian007:~/testTime$ locate Python.h
/usr/include/python2.5_d/Python.h
/usr/include/python2.5/Python.h
原来在这个位置呀(后面要用到),先编写一个功能模块:
example.c

#include

#include "time.h"

 

#define num 1000000000

const int global = 123;

 

void testFor()

{

    int i = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       i += 1;

    }

    time2 = time(NULL);

    printf("for-loop:\t%f\n",difftime(time2,time1));

}

 

void testAdd()

{

    int i = 0;

    int value1 = 123;

    int value2 = 234;

    int value3 = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value3 = value1 + value2;

       i += 1;

    }

    time2 = time(NULL);

    printf("add:\t%f\n",difftime(time2,time1));

}

 

void testSubtract()

{

    int i = 0;

    int value1 = 123;

    int value2 = 234;

    int value3 = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value3 = value2 - value1;

       i += 1;

    }

    time2 = time(NULL);

    printf("subtract:\t%f\n",difftime(time2,time1));

}

 

void testMultiply()

{

    int i = 0;

    int value1 = 123;

    int value2 = 234;

    int value3 = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value3 = value1 * value2;

       i += 1;

    }

    time2 = time(NULL);

    printf("multiply:\t%f\n",difftime(time2,time1));

}

 

void testDivide()

{

    int i = 0;

    int value1 = 123;

    int value2 = 234;

    int value3 = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value3 = value1 /  value2;

       i += 1;

    }

    time2 = time(NULL);

    printf("divide:\t%f\n",difftime(time2,time1));

}

 

void testDivide2()

{

    int i = 0;

    int value1 = 123;

    int value2 = 234;

    int value3 = 0;

    int temp = (1/value1);

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value3 = value2 * temp;

       i += 1;

    }

    time2 = time(NULL);

    printf("divide2:\t%f\n",difftime(time2,time1));

}

 

void testVar()

{

    int i = 0;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       int a = 1;

       int b = 2;

       int c = 3;

       i += 1;

    }

    time2 = time(NULL);

    printf("var:\t%f\n",difftime(time2,time1));

}

 

void testIf()

{

    int i = 0;

    int value = 123;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       if(value < 12)

           continue;

       i += 1;

    }

    time2 = time(NULL);

    printf("if:\t%f\n",difftime(time2,time1));

}

 

void testReadGlobal()

{

    int i = 0;

    int value = 123;

    time_t time1,time2;

    time1 = time(NULL);

    while(i < num){

       value = global;

       i += 1;

    }

    time2 = time(NULL);

    printf("readGlobal:\t%f\n",difftime(time2,time1));

}

然后再编写对应文件来包裹功能模块:
wrapper.c

#include

 

PyObject* wrap_testFor(PyObject* self,PyObject* args)

{

    testFor();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testAdd(PyObject* self,PyObject* args)

{

    testAdd();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testSubtract(PyObject* self,PyObject* args)

{

    testSubtract();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testMultiply(PyObject* self,PyObject* args)

{

    testMultiply();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testDivide(PyObject* self,PyObject* args)

{

    testDivide();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testDivide2(PyObject* self,PyObject* args)

{

    testDivide2();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testVar(PyObject* self,PyObject* args)

{

    testVar();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testIf(PyObject* self,PyObject* args)

{

    testIf();

    Py_INCREF(Py_None);

    return Py_None;

}

 

PyObject* wrap_testReadGlobal(PyObject* self,PyObject* args)

{

    testReadGlobal();

    Py_INCREF(Py_None);

    return Py_None;

}

 

static PyMethodDef exampleMethods[] = {

    {"testFor",wrap_testFor,METH_VARARGS,"test For() time"},

    {"testAdd",wrap_testAdd,METH_VARARGS,"test Add() time"},

    {"testSubtract",wrap_testSubtract,METH_VARARGS,"test Subtract() time"},

    {"testMultiply",wrap_testMultiply,METH_VARARGS,"test Multiply() time"},

    {"testDivide",wrap_testDivide,METH_VARARGS,"test Divide() time"},

    {"testDivide2",wrap_testDivide2,METH_VARARGS,"test Divide2() time"},

    {"testVar",wrap_testVar,METH_VARARGS,"test Var() time"},

    {"testIf",wrap_testIf,METH_VARARGS,"test If() time"},

    {"testReadGlobal",wrap_testReadGlobal,METH_VARARGS,"test ReadGlobal() time"},

    {NULL,NULL}

};

 

void initexample()

{

    PyObject* m;

    m = Py_InitModule("example",exampleMethods);

}

编译这些模块,以便提供给python使用:
lsj@debian007:~/testTime$ gcc -fpic -c -l/usr/include/python2.5 -l/usr/lib/python2.5/config example.c wrapper.c
lsj@debian007:~/testTime$ gcc -shared -o example.so example.o wrapper.o
最后重新编写一个testTime.py:

#!/usr/bin/python

#-*- coding: utf-8 -*-

 

import example

 

if __name__=='__main__':

    example.testFor()

    example.testAdd()

    example.testSubtract()

    example.testMultiply()

    example.testDivide()

    example.testDivide2()

    example.testVar()

    example.testIf()

    example.testReadGlobal()

嗯,现在的结果令我比较满意:
lsj@debian007:~/testTime$ ./testTime.py
for-loop:       4.000000
add:    4.000000
subtract:       5.000000
multiply:       4.000000
divide: 8.000000
divide2:        5.000000
var:    4.000000
if:     4.000000
readGlobal:     4.000000

如何使用,就仁者见仁,智者见智了哟
阅读(1727) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-09-20 09:09:21

仁者见仁,智者见智,多多。