Calculate program run time in C, Java, and Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.