Favor object composition over class inheritance.
by Gang of Four
+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: How to find code execution time

  1. #1
    Super Moderator human's Avatar
    Join Date
    Apr 2004
    Location
    India
    Posts
    642

    How to find code execution time

    If you are a PHP developer chances are many that you need to find how much time it took to execute a piece of code. Using the script below you can find how much time it took to execute your php code.

    Code:
    $time_start = microtime(true);
    
    // execute your tasks here
    
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    
    echo "Code executed in $time seconds\n";
    Via
    Thamasoma jyothirgamaya, Asathoma sath gamaya, Mrithyoma amrutham gamaya || Lead us from illusion to Truth, from darkness to Light, from transience to Permanence

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Location
    somewhere
    Posts
    116
    Nice, I use it a lot...

    ASP (VBScript).
    Code:
    <%
    	Dim StartTime, EndTime, TotalTime
    	StartTime = Timer() 
    
    	'execute code
    
    	EndTime = Timer() 
    
    	TotalTime = EndTime-StartTime 'The result is given in seconds with miliseconds precision
    %>
    C#
    Code:
    	var sw = new Stopwatch();
    	sw.Start();
    
    	//execute code
    
    	Console.WriteLine( "Execution time: {0} ms", sw.ElapsedMilliseconds );
    	sw.Stop();
    Last edited by c4st0r; 01-13-2011 at 08:38 PM.

  3. #3
    Super Moderator human's Avatar
    Join Date
    Apr 2004
    Location
    India
    Posts
    642
    Thx c4st0r, a link to this page is added in the original page for ASP and C# versions of the code.


    Can we add the code for other languages also here, so any developer who looks of similar code in their language of choice should come here?
    Last edited by human; 01-12-2011 at 05:30 AM.
    Thamasoma jyothirgamaya, Asathoma sath gamaya, Mrithyoma amrutham gamaya || Lead us from illusion to Truth, from darkness to Light, from transience to Permanence

  4. #4
    Super Moderator human's Avatar
    Join Date
    Apr 2004
    Location
    India
    Posts
    642
    For Javascript you can use this code.
    Code:
    var startDate = new Date();
    
    // execute your tasks here
    
    var endDate = new Date();
    
    var timeTaken = endDate.getTime() - startDate.getTime();
    
    alert('Time take to execute the script is '+timeTaken+' milliseconds');
    Thamasoma jyothirgamaya, Asathoma sath gamaya, Mrithyoma amrutham gamaya || Lead us from illusion to Truth, from darkness to Light, from transience to Permanence

  5. #5
    Junior Member
    Join Date
    Apr 2003
    Location
    Philippines
    Posts
    10

    Post sample, execution time for vbscript.

    Code:
    startTime = Now
       'H = Hours
       'N = Minutes
       'S = Seconds
    endTime = DateAdd("S",60,startTime)
    
    Do Until Now >= endTime
        WScript.Echo Now
    Loop
    Last edited by human; 01-12-2011 at 11:23 AM. Reason: Added code tags
    Cyber Evolution, Keeps Evolving...

  6. #6
    Senior Member
    Join Date
    Jan 2011
    Location
    somewhere
    Posts
    116
    Quote Originally Posted by looserpedro View Post
    Code:
    startTime = Now
       'H = Hours
       'N = Minutes
       'S = Seconds
    endTime = DateAdd("S",60,startTime)
    
    Do Until Now >= endTime
        WScript.Echo Now
    Loop

    Hey looserpedro,

    Actually your code isn't gonna work for it, it's more like a "Pause Script" that will make the server looping for 60 seconds before continue the execution.

    What we are showing here is scripts that identify the time of execution of the script, if you input a vbscript code to verify the execution time in your asp.net (vbscript) code, it probably gonna show exactly the 60s.

    BTW, I would never use a loop for this kind of thing, even if you want to "pause" for awhile, loops like this takes the CPU use to 100%, its more like a DoS :P

    cheers

  7. #7
    Administrator bulibuta's Avatar
    Join Date
    Aug 2004
    Location
    Romania
    Posts
    2,210
    Human, seeing how this developed into a multi-language topic, you might want to change the thread's title then.
    Everything is simple, we're stupid.
    gopher://sdf.lonestar.org/1/user/bulibuta

  8. #8
    Super Moderator human's Avatar
    Join Date
    Apr 2004
    Location
    India
    Posts
    642
    Thx buli for noticing, changed.

    Can anyone provide code for C,C++, ASM, Java, Perl, Pyton, Ruby, Shell script etc...
    Thamasoma jyothirgamaya, Asathoma sath gamaya, Mrithyoma amrutham gamaya || Lead us from illusion to Truth, from darkness to Light, from transience to Permanence

  9. #9
    Here is a small code for python
    Code:
    import time
    from math import sqrt
    def getMedian(numericValues): # From http://codecomments.wordpress.com/2008/03/17/simple-method-to-calculate-median-in-python/
            theValues = sorted(numericValues)
    
            if len(theValues) % 2 == 1:
                    return theValues[(len(theValues)+1)/2-1]
            else:
                    lower = theValues[len(theValues)/2-1]
                    upper = theValues[len(theValues)/2]
                    return (float(lower + upper)) / 2
    def meanstdv(x): # From http://www.phys.uu.nl/~haque/computing/WPark_recipes_in_python.html
            n, mean, std = len(x), 0, 0
            for a in x:
                    mean = mean + a
                    mean = mean / float(n)
                    for a in x:
                            std = std + (a - mean)**2
                            std = sqrt(std / float(n-1))
            return mean, std
    
    def _TimeIt(func,*args,**kwargs):
            s_time = time.time()
            func(*args,**kwargs)
            e_time = time.time()
            return e_time - s_time
    def TimeIt(func,*args,**kwargs):
            print "Execution time %f seconds" % _TimeIt(func,*args,**kwargs)
    
    def TimeItStat(n,func,*args,**kwargs):
            if n <= 0: raise ValueError("n must be greater than zero")
            times = []
            for i in range(n):
                    times.append( _TimeIt(func,*args,**kwargs) )
            mean, sd = meanstdv(times)
            med = getMedian(times)
            print "Execution time mean=%f med=%f best=%f worst=%f sd=%f" % (mean,med,min(times),max(times),sd)
    
    # Example usage
    def main(runTimes,hello=""):
            for i in range(runTimes):
                    hello += hello
    
    if __name__ == "__main__":
            TimeIt(main,25,hello="world")
            TimeItStat(10,main,25,hello="world")
    And the output
    $ python timeit.py
    Execution time 0.465171 seconds
    Execution time mean=0.052075 med=0.468760 best=0.468105 worst=0.469439 sd=0.205118
    Last edited by noen; 01-13-2011 at 08:54 AM.
    I'm not a bright noxious light

  10. #10
    Senior Member
    Join Date
    Jan 2011
    Location
    somewhere
    Posts
    116
    Python Simplified...

    Code:
    import time
    
    startTime = time.clock()
    
    #code, function, whatever you wanna mesure
    
    endTime = time.clock()
    print 'Code time %.6f seconds' % (endTime - startTime)
    cheers
    Last edited by c4st0r; 01-13-2011 at 08:53 AM.

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 02-11-2009, 03:20 PM
  2. finding program execution time
    By Data in forum Tech Talk
    Replies: 5
    Last Post: 05-13-2004, 08:27 AM
  3. Replies: 2
    Last Post: 07-26-2001, 03:12 AM
  4. Replies: 1
    Last Post: 07-26-2001, 03:01 AM
  5. Replies: 0
    Last Post: 07-26-2001, 02:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
<<<<<<<< Your Customized Value <<<<<<<<