分类:
2008-09-07 15:18:33
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 |
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 |
#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)); } |
#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); } |
#!/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 |