CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2000
    Location
    india
    Posts
    191

    Time needed for execution

    Hi,

    How to find out the time taken for the execution of a piece of code. For example, consider the following sample code:

    for i = 0 to 50000
    Debug.print i
    next



    How to findout the execution time?

    Thanks in advance



  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Time needed for execution


    'You should use gettickcount Api before and avfter execution of code and than
    'transform the difference in time.
    'Beware: your machine will give different results each time, as your cpu will
    'have different task /usage each time due to other kind of job doing in the
    'meanwhile...

    option Explicit
    'In general section
    private Declare Function GetTickCount Lib "kernel32" () as Long

    private Sub Command1_Click()
    Dim retStart as Long, retEnd as Long
    Dim lngI as Long
    Dim totMillisecod as Long
    retStart = GetTickCount
    for lngI = 0 to 50000
    Debug.print lngI
    next
    retEnd = GetTickCount
    'number of millisecond elapsed:
    totMillisecod = retEnd - retStart
    MsgBox "millisecond elapsed=" & totMillisecod & _
    " Secod elapsed=" & totMillisecod / 1000

    End Sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Aug 2000
    Location
    india
    Posts
    191

    Re: Time needed for execution

    Thanks Cimperiali for the response.

    Now, I have got a different question, though not directly related with my previous question. When I execute the sample code mentioned in my question with a for loop of 50,000 cycles, it gives a runtime error 'Overflow'. This error generally comes with looping commands involving large cycles. But, we may have to execute large cycles in our projects. How to overcome this error?

    Thanks in advance.


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Time needed for execution

    I saw you wrote:
    for i = 0 to ...
    i is usally dimensioned as Integer tYpe (=max value = 32.000).
    You need a Long type.
    So, use a :
    dim lngI as long
    for lngI = 0 to 50.000
    ...


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Aug 2000
    Location
    india
    Posts
    191

    Re: Time needed for execution

    You are right. i should have been long type indeed!!!
    Many Thanks.


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Happy it helped

    Have a nice day and happy coding.

    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jan 2002
    Location
    Seattle, Washington
    Posts
    84

    Re: Time needed for execution

    One thing to watch out for here is if the tick counter wraps back to zero. Here's a little excerpt out of MSDN:

    -------------------------------------------------
    The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

    The following example demonstrates how to handle timer wrap around.


    DWORD dwStart = GetTickCount();

    // Stop if this has taken too long
    if( GetTickCount() - dwStart >= TIMELIMIT )
    Cancel();
    -------------------------------------------------


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured