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(); //需要测试运行时间的代码段放在这 finish = clock(); totalTime = (double)(finish - start); cout<<"花费"<<totalTime<<"毫秒"<<endl; return 0; }
#include <iostream> #include <Windows.h> using namespace std; int main() { LONGLONG start, finish; LONGLONG totalTime; start = GetTickCount(); //需要测试运行时间的代码段放在这 finish = GetTickCount(); totalTime = finish - start; cout<<"花费"<<totalTime<<"毫秒"<<endl; return 0; }
//伪代码 long start=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 long end=System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间: "+(end-start)+"ms");
//伪代码 long start=System.nanoTime(); //获取开始时间 doSomeThing(); //测试的代码段 long end=System.nanoTime(); //获取结束时间 System.out.println("程序运行时间: "+(end-start)+"ns");
from time import clock start=clock() //code finish=clock() print (finish-start)/1000000
# -*- coding: utf-8 -*- #!/bin/env python def test1(): n=0 for i in range(101): n+=i return n def 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)
原文地址:http://blog.csdn.net/tanzhangwen/article/details/40787903