#include <stdio.h> #include <stdlib.h> #include <string.h> #include<time.h> using namespace std; int main() { int start,finish,a=1; start = clock(); for(int i=1;i<=10000000;++i){ a=a+1; } finish = clock(); double TotTime = (double)(finish-start)/CLOCKS_PER_SEC; printf("The program cost %f seconds. ",TotTime); }
//0.02000s
package lj; public class TimeTest { public static void main(String args[]) { int a = 0; long startTime = System.currentTimeMillis();//获取当前时间 for(int i = 1;i <= 1000000 ; i++) { a=a+1; } long endTime = System.currentTimeMillis(); System.out.println("程序运行时间:"+(endTime-startTime)+"ms"); } } //程序运行时间:3ms //0.003s
#@author:lj 通过循环比较一百万次加法表达式运算时间 import datetime a = 0 start = datetime.datetime.now() for i in range(1,1000000): a=a+1 end=datetime.datetime.now() print('Running time: %s Seconds'%(end-start)) #Running time: 0:00:00.130616 Seconds
简单记录一下这几个函数调用