C,C++和Python的性能比较(1)
时间:2008-09-23 来源:dengguojin001
testTime.cpp
testTime.py
C++语言: testTime.cpp
#include <iostream> #include <string> #include "time.h" using namespace std; #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); cout << "for-loop:\t" << difftime(time2,time1) << endl; } 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); cout << "add:\t" << difftime(time2,time1) << endl; } 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); cout << "subtract:\t" << difftime(time2,time1) << endl; } 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); cout << "multiply:\t" << difftime(time2,time1) << endl; } 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); cout << "divide:\t" << difftime(time2,time1) << endl; } 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); cout << "divide2:\t" << difftime(time2,time1) << endl; } 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); cout << "var:\t" << difftime(time2,time1) << endl; } 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); cout << "if:\t" << difftime(time2,time1) << endl; } 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); cout << "readGlobal:\t" << difftime(time2,time1) << endl; } void testConcatStrings() { int i = 0; string string1 = "abcdefghijklmnopqrstuvxyz"; string string2 = "abcdefghijklmnopqrstuvxyz"; string string3 = ""; time_t time1,time2; time1 = time(NULL); while(i < num){ string3 = string1 + string2; i += 1; } time2 = time(NULL); cout << "concatStrings:\t" << difftime(time2,time1) << endl; } int main() { testFor(); testAdd(); testSubtract(); testMultiply(); testDivide(); testDivide2(); testVar(); testIf(); testReadGlobal(); testConcatStrings(); return 0; } |
testTime.py
Python语言: testTime.py
#!/usr/bin/python #-*- coding:utf-8 -*- import time num = 1000000000 gValue = 123 def testFor(): i = 0 time1 = time.localtime() while i < num: i += 1 time2 = time.localtime() print 'for-loop:\t',time.mktime(time2) - time.mktime(time1) def testAdd(): i = 0 value1 = 123 value2 = 234 value3 = 0 time1 = time.localtime() while i < num: value3 = value1 + value2 i += 1 time2 = time.localtime() print 'add:\t',time.mktime(time2) - time.mktime(time1) def testSubtract(): i = 0 value1 = 123 value2 = 234 value3 = 0 time1 = time.localtime() while i < num: value3 = value2 - value1 i += 1 time2 = time.localtime() print 'substract:\t',time.mktime(time2) - time.mktime(time1) def testMultiply(): i = 0 value1 = 123 value2 = 234 value3 = 0 time1 = time.localtime() while i < num: value3 = value2 * value1 i += 1 time2 = time.localtime() print 'multiply:\t',time.mktime(time2) - time.mktime(time1) def testDivide(): i = 0 value1 = 123 value2 = 234 value3 = 0 time1 = time.localtime() while i < num: value3 = value2 / value1 i += 1 time2 = time.localtime() print 'divide:\t',time.mktime(time2) - time.mktime(time1) def testDivide2(): i = 0 value1 = 123 value2 = 234 value3 = 0 temp = (1/value1) time1 = time.localtime() while i < num: value3 = value2 * temp i += 1 time2 = time.localtime() print 'divide2:\t',time.mktime(time2) - time.mktime(time1) def testVar(): i = 0 time1 = time.localtime() while i < num: a = 1 b = 2 c = 3 i += 1 time2 = time.localtime() print 'var:\t',time.mktime(time2) - time.mktime(time1) def testIf(): i = 0 value1 = 123 time1 = time.localtime() while i < num: if value1 < 12: pass i += 1 time2 = time.localtime() print 'if:\t',time.mktime(time2) - time.mktime(time1) def testReadGlobal(): i = 0 value1 = 123 time1 = time.localtime() while i < num: value1 = gValue i += 1 time2 = time.localtime() print 'readGlobal:\t',time.mktime(time2) - time.mktime(time1) def testConcatStrings(): i = 0 string1 = 'abcdefghijklmnopqrstuvxyz' string2 = 'abcdefghijklmnopqrstuvxyz' string3 = '' time1 = time.localtime() while i < num: string3 = string1 + string2 i += 1 time2 = time.localtime() print 'concatStrings:\t',time.mktime(time2) - time.mktime(time1) if __name__=='__main__': testFor() testAdd() testSubtract() testMultiply() testDivide() testDivide2() testVar() testIf() testReadGlobal() testConcatStrings() |
相关阅读 更多 +