C + +
Http://wenku.baidu.com/view/9e6f4548852458fb770b56c7.html
#include <iostream> #include <time.h>using namespace std; int main () {clock_t start, finish; Double totaltime; start = Clock (); The code snippet that requires test run time is placed in this finish = Clock (); TotalTime = (double) (Finish-start); cout<< "spend" <<totalTime<< "milliseconds" <<endl; return 0; }
#include <iostream> #include <windows.h>using namespace std; int main () {Longlong start, finish; Longlong TotalTime; Start = GetTickCount (); The code snippet that requires a test run time is placed in this finish = GetTickCount (); TotalTime = Finish-start; cout<< "spend" <<totalTime<< "milliseconds" <<endl; return 0;}
Java
Pseudo code long Start=system.currenttimemillis (); Get start time dosomething ();
nanosecond units
Pseudo code long Start=system.nanotime (); Get start time dosomething ();
Python
From time import clock start=clock () //code finish=clock () print (finish-start)/1000000
Http://www.cnblogs.com/moinmoin/archive/2011/03/18/python-runtime-measuring.html
#-*-Coding:utf-8-*-#!/bin/env pythondef test1 (): n=0 for i in range (101): n+=i return ndef test2 ():
return sum (range (101)) def test3 (): return sum (x for X in range (101)) if __name__== ' __main__ ': from Timeit Import Timer T1=timer ("Test1 ()", "from __main__ import test1") T2=timer ("Test2 ()", "from __main__ import Test2" ) T3=timer ("Test3 ()", "from __main__ import test3") print T1.timeit (1000000) print T2.timeit (1000000) Print T3.timeit (1000000) print t1.repeat (3,1000000) print t2.repeat (3,1000000) Print T3.repeat (3,1000000)
Calculate program run time in C, Java, and Python