home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / timer / c_timr20 / c-timer.doc next >
Encoding:
Text File  |  1992-06-10  |  13.4 KB  |  367 lines

  1.                               C-Timer version 2.0
  2.  
  3.                                     6-10-92
  4.  
  5.                                       by
  6.  
  7.                                   J  Podolak
  8.  
  9.  
  10. INTRODUCTION:
  11.  
  12.    C-timer is a C code optimization tool written for Microsoft C that allows
  13.    the programmer to measure with great accuracy the execution time of a C
  14.    function call. C-timer can measure both library functions and custom
  15.    written functions, and provides results in both time per call and calls
  16.    per second. C-timer provides results with an accuracy of one microsecond.
  17.  
  18.    C-timer is an invaluable tool for determining how to write the fastest
  19.    possible code, and can show the effects of compiler optimizations on
  20.    code execution time.
  21.  
  22.  
  23. FEATURES:
  24.  
  25.    User modifiable setup function allows the programmer to access command line
  26.       parameters and prepare variables before testing the function.
  27.  
  28.    Output redirectable to a log file using standard DOS redirection commands.
  29.  
  30.    Timing range from 1 microsecond to 20 seconds.
  31.  
  32.    Automatically calls fast functions up to 64000 times and subtracts function
  33.       call overhead to achieve accurate results on fast functions.
  34.  
  35.    Compensates for the number of arguments of the test function (0 - 5) and
  36.       subtracts argument pushing overhead to provide accurate results.
  37.  
  38.    Runs PC timer at ten times the normal speed to provide better timing
  39.       granularity.
  40.  
  41.  
  42. PACKAGE CONTENTS:
  43.  
  44.     C-timer.doc - This file, the documentation for the C-timer package.
  45.     C-timer.inf - Update history & registration information.
  46.    C-timerS.obj - The Microsoft C SMALL model .obj module.
  47.    C-timerL.obj - The Microsoft C LARGE model .obj module.
  48.    Userfunc.c   - A sample .c module illustrating how to use C-timer.
  49.    C-timer.src  - A password encoded zip file of source for C-timer.obj.
  50.                   REGISTERED users will receive the password for this zip.
  51.  
  52. DESCRIPTION & USE:
  53.  
  54.    C-timer was written to allow easy implementation of testing for any
  55.    function, and provides machine-independent execution timings. C-timer
  56.    consists of an .obj module (provided in SMALL and LARGE models), and
  57.    a simple .c module which the programmer must modify to call the desired
  58.    test function.
  59.  
  60.    To use C-timer, the programmer must modify the .c file to call the desired
  61.    test function, then compile the .c file and link it to the C-timer .obj
  62.    file (C-timerS.obj for SMALL model, C-timerL.obj for LARGE model). The
  63.    resulting .exe file is then run to provide execution timing information.
  64.  
  65.    The sample .c module provided is called userfunc.c and provides a template
  66.    for how to use the C-timer package. Please refer to this file for more
  67.    clarification of the procedures discussed.
  68.  
  69.  
  70. A SAMPLE SESSION:
  71.  
  72.    Suppose you have written a new strcpy() routine, and would like to test
  73.    it for speed. Assume that your function looks like this:
  74.  
  75.       void my_strcpy(char *dest, char *srce)
  76.       {
  77.          while(*srce)
  78.             *dest++ = *srce++;
  79.  
  80.       }
  81.  
  82.    Edit the file userfunc.c and do the following:
  83.  
  84.       1. Near the top of the file, replace the line:
  85.  
  86.             void userfunc(unsigned int a, unsigned int b, unsigned int c);
  87.  
  88.          with
  89.  
  90.             void my_strcpy(char *, char *);
  91.  
  92.          Now you have prototyped the function.
  93.  
  94.  
  95.       2. Go to the function_call() routine and replace the line:
  96.  
  97.             userfunc(arg1, arg2, arg3);
  98.  
  99.          with
  100.  
  101.             my_strcpy(new_string, old_string);
  102.  
  103.          Now you have told C-timer to call your function, my_strcpy().
  104.  
  105.  
  106.       3. Near to the top of the file, replace the three lines:
  107.  
  108.             unsigned int arg1;
  109.             unsigned int arg2;
  110.             unsigned int arg3;
  111.  
  112.          with
  113.  
  114.             char new_string[80];
  115.             char old_string[80];
  116.  
  117.          Now you have created the variables needed for your function.
  118.  
  119.       4. Go to the setup_userfunc() routine and add the following line
  120.          above the closing brace of the routine:
  121.  
  122.             sprintf(old_string, "This is a test string of 34 chars.");
  123.  
  124.          Now you have prepared the variable old_string for your function,
  125.          my_strcpy. If you wish, you may delete the other lines in
  126.          setup_userfunc(), they simply print out the command line (if you do
  127.          this, and you have the compiler set to warning level 4, you may see
  128.          compilation warnings about unreferenced formal parameters argc,
  129.          and argv; this is a harmless warning).
  130.  
  131.       5. Go to the userfunc() routine and delete it completely, replacing it
  132.          with your function, for example:
  133.  
  134.             void my_strcpy(char *dest, char *srce)
  135.             {
  136.                while(*srce)
  137.                   *dest++ = *srce++;
  138.  
  139.             }
  140.  
  141.           ADVANCED NOTE: Alternatively, you may place the test function in a
  142.                          separately compiled module, as long as you prototype
  143.                          it and call it in function_call().
  144.  
  145.  
  146.       6. Save the file userfunc.c.
  147.  
  148.       7. Compile the file userfunc.c, for example:
  149.  
  150.             cl /c userfunc.c
  151.  
  152.       8. Link the userfunc.obj file to the c-timerS.obj (c-timerL.obj if
  153.          you are using LARGE model), for example:
  154.  
  155.             link userfunc + c-timers, mytest.exe;
  156.  
  157.          This will produce an executable called mytest.exe.
  158.  
  159.       9. Run the program mytest.exe with 2 command line parameters, for
  160.          example:
  161.  
  162.             mytest test_of_my_strcpy_34 2
  163.  
  164.          The first parameter is a text label for C-timer to identify
  165.          your test function with. The second parameter is the number of
  166.          arguments that your test function takes (in this case, two).
  167.  
  168.      10. View the results of the timing test for my_strcpy(). On a 20Mhz
  169.          386, the results were:
  170.  
  171.             c:\ mytest test_of_my_strcpy_34 2
  172.             Timing test_of_my_strcpy_34(), 2 arguments.
  173.                     This takes up to 20 seconds...Done.
  174.                                                                                 
  175.             test_of_my_strcpy_34() takes 0.086 ms/call, 11627 calls/sec.
  176.                     64000 calls at 182 interrupts/second
  177.                                                                                 
  178.             c:\
  179.  
  180.          This means that your function, my_strcpy(), takes 86 microseconds
  181.          per call, when called to copy 34 characters, and can be called
  182.          11627 times per second. The next line says that C-timer timed your
  183.          function 64000 times at 182 clock ticks per second (ten times the
  184.          standard clock rate) to arrive at this result.
  185.  
  186.    This seems pretty fast, but lets compare it to the standard library
  187.    strcpy() function. Follow the steps below:
  188.  
  189.       Edit the file userfunc.c and do the following:
  190.  
  191.       1. Near the top of the file, add the line:
  192.  
  193.             #include <string.h>
  194.  
  195.          Now you have provided the prototype for the library strcpy() function.
  196.  
  197.  
  198.       2. Go to the function_call() routine and replace the line:
  199.  
  200.             my_strcpy(new_string, old_string);
  201.  
  202.          with
  203.  
  204.             strcpy(new_string, old_string);
  205.  
  206.          Now you have told C-timer to call the standard library function,
  207.          strcpy().
  208.  
  209.       3. Save the file userfunc.c.
  210.  
  211.       4. Compile the file userfunc.c, for example:
  212.  
  213.             cl /c userfunc.c
  214.  
  215.       5. Link the userfunc.obj file to the c-timerS.obj (c-timerL.obj if
  216.          you are using LARGE model), for example:
  217.  
  218.             link userfunc + c-timers, stdtest.exe;
  219.  
  220.          This will produce an executable called stdtest.exe.
  221.  
  222.       9. Run the program stdtest.exe with 2 command line parameters, for
  223.          example:
  224.  
  225.             stdtest test_of_strcpy_34 2
  226.  
  227.          The first parameter is a text label for C-timer to identify
  228.          your test function with. The second parameter is the number of
  229.          arguments that your test function takes (in this case, two).
  230.  
  231.       10.View the results of the timing test for strcpy(). On a 20Mhz 386,
  232.          the results were:
  233.  
  234.             c:\ stdtest test_of_strcpy_34 2
  235.             Timing test_of_strcpy_34(), 2 arguments.
  236.                     This takes up to 20 seconds...Done.
  237.                                                                                 
  238.             test_of_strcpy_34() takes 0.018 ms/call, 55555 calls/sec.
  239.                     64000 calls at 182 interrupts/second
  240.                                                                                 
  241.             c:\
  242.  
  243.          This means that the standard library function, strcpy(), takes only
  244.          18 microseconds per call, when called to copy 34 characters, and can
  245.          be called 55555 times per second. The next line says that C-timer
  246.          timed the function 64000 times at 182 clock ticks per second (ten
  247.          times the standard clock rate) to arrive at this result.
  248.  
  249.    So it would seem that the standard library strcpy() function is about 5
  250.    times faster than your test function, my_strcpy(). Why? The library function
  251.    was written in assembly language. You could probably come close to the
  252.    speed of the library function if you worked in assembly language, but why
  253.    reinvent the wheel? Incidentally, if you do write an assembly language
  254.    routine and assemble it with Microsoft assembler, you can link it to the
  255.    C-timer and userfunc modules to test its execution time, as well. Just
  256.    follow the rules for writing an assembler routine to be called from C.
  257.  
  258.    If you would like a more permanent copy of the timing results, try using
  259.    the standard DOS redirection commands. In the examples given above, the
  260.    command lines would be:
  261.  
  262.       mytest test_of_my_strcpy_34 2 >> test.log
  263.  
  264.    and:
  265.  
  266.       stdtest test_of_strcpy_34 2 >> test.log
  267.  
  268.    This would produce a file called test.log which would contain the lines:
  269.  
  270.       Timing test_of_my_strcpy_34(), 2 arguments.
  271.  
  272.       test_of_my_strcpy_34() takes 0.086 ms/call, 11627 calls/sec.
  273.               64000 calls at 182 interrupts/second
  274.  
  275.       Timing test_of_strcpy_34(), 2 arguments.
  276.  
  277.       test_of_strcpy_34() takes 0.018 ms/call, 55555 calls/sec.
  278.               64000 calls at 182 interrupts/second
  279.  
  280.    Now you can see the value of the text label command line parameter, it
  281.    makes log files easy to read, especially if you are testing a large
  282.    number of functions.
  283.  
  284.    Try compiling with different optimizations to increase the speed of your
  285.    functions, and use C-timer to generate a log file of the timing results.
  286.    If you use the text label parameter creatively, you can easily log your
  287.    results (try 'mytest "my_strcpy34 Oi Oa " 2 >> test.log' for example, to
  288.    log compiler options.
  289.  
  290.  
  291. USERFUNC.C DESCRIPTION:
  292.  
  293.    This file contains a skeletal example of how to use C-timer. There are two
  294.    functions that must be linked with C-timer, and examples are provided in
  295.    userfunc.c ( setup_userfunc() and function_call() ). Below is a description
  296.    of each of the two functions.
  297.  
  298.  
  299.    setup_userfunc() - This function is called ONCE by C-timer, prior to
  300.                       beginning the timing procedure. It is passed the command
  301.                       line (argc and argv) for you to make use of. Keep in mind
  302.                       that argv[0] is the name of C-timer, argv[1] is the
  303.                       name of the test function (or other text string), and
  304.                       argv[2] is the number of parameters to the test function.
  305.                       If your function needs additional command line parameters,
  306.                       read them here as argv[3] through argv[n].
  307.  
  308.                       This sample function just prints out the command line.
  309.                       If you have no need for a setup routine, you must still
  310.                       provide this function, although it may consist of only
  311.                       empty {}.
  312.  
  313.  
  314.    function_call() -  This function is called repeatedly by C-timer during
  315.                       the timing procedure. It should contain only a call to
  316.                       the test function. Any additional code you need to
  317.                       execute before calling your test function should be
  318.                       placed in setup_userfunc(), described above.
  319.  
  320.                       You must provide this function, although the test
  321.                       function may be called whatever you like, as long as
  322.                       you prototype it near the top of the file.
  323.  
  324.  
  325. CHANGES SINCE VERSION 1.0:
  326.  
  327.    Protects against Ctrl-C interrupts.
  328.  
  329.    Addition of a setup function to allow programmer to prepare variables.
  330.  
  331.    Command line now available to programmer to allow user-specific command
  332.       line parameters.
  333.  
  334.    Output redirection via standard DOS redirection commands.
  335.  
  336.  
  337. KNOWN BUGS:
  338.  
  339.    None.
  340.  
  341.  
  342. DISCLAIMER:
  343.  
  344.    The author assumes no responsibility for the use of this program, as it
  345.    is the users responsibility to determine the applicability and safety of
  346.    the application.
  347.  
  348.  
  349. REGISTRATION:
  350.  
  351.    This program is NOT free, but is user-supported-software.
  352.    If you find this program useful, please send $10.00 to:
  353.  
  354.       J Podolak
  355.       25W037 Keswick Lane
  356.       Naperville, Illinois 60540
  357.  
  358.    Registration will provide you with the password for the zip source, and
  359.    free updates to this program.
  360.  
  361.  
  362. COMMENTS:
  363.  
  364.    If you have any comments or questions about the program, please leave
  365.    a message for J Podolak on America Online, or write to the address
  366.    above.
  367.