1. 程式人生 > 實用技巧 >python、c、java統計時間

python、c、java統計時間

#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.\n",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

簡單記錄一下這幾個函式呼叫