Results 1 to 2 of 2

Thread: Code Timing

  1. #1

    Thread Starter
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Code Timing

    When trying to find out what code works better, it's always a good idea to time your code. There is about 3 different ways to go about doing this, I'm going to show you one of them here. (Though I might come back later and show you other ways).

    Timing *acurate* code in a multi-tasking enviroment can be extremely hard, in fact, it's almost impossible to do. The reason is because there is different programs running on your computer and they all share the same processor. When it's another programs turn to use the cpu, your code is interupted until it gets another turn at the cpu.

    The following method of timing code is very popular with programmers because it's very simple to do and produces enough accuracy to where you can tell what algorithm works better.

    First - The API's you will need:
    Code:
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Private Const REALTIME_PRIORITY_CLASS = &H100
    Private Const NORMAL_PRIORITY_CLASS = &H20
    In the following example, you will need a picture box (Default Name) to display the results of the code in.

    Code:
       Dim start As Long
        Dim finish As Long
        Dim i As Long
        Dim hProcess As Long
        Dim b As Long
        
        hProcess = GetCurrentProcess
        SetPriorityClass hProcess, REALTIME_PRIORITY_CLASS
        
        start = GetTickCount
                 
        For i = 1 To 1000000
            ''''''''''''''''''''''
            'Timed Code Goes Here'
            ''''''''''''''''''''''
            b = b + 5
        Next i
        
        finish = GetTickCount
        SetPriorityClass hProcess, NORMAL_PRIORITY_CLASS
        
        Picture1.Print (finish - start)
    Some of you may wonder why we change the processor priority for our example. We do this because we are in a multi-tasking enviroment and we want to ***** cpu time as much as possible for the sake of accuracy. Try it without setting the process to high, you'll notice that the results jump quite a bit more.

    Note: Just know that you should change the number of loop iterations depending on your algorithm. This example used code so small that I had to use a large number of loop iterations to get results. A large algorithm may take a very long time with that many iterations, so back down on it when timing code and then raise it till you get results.
    Last edited by Maven; Dec 22nd, 2004 at 03:42 AM.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  2. #2

    Thread Starter
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Re: Code Timing

    what is up with the backlash?
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

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