测试cpu的简单工具-dhrystone
发布日期:2021-06-29 02:34:56 浏览次数:3 分类:技术文章

本文共 27097 字,大约阅读时间需要 90 分钟。

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】 

 

    在现在的internet网站上,有很多的cpu性能测试工具。这其中包括了各种评分软件,但是我还是比较中意dhrystone这份软件。说是软件,其实也就是三个文件,分别是dhry.h、dhry_1.c、dhry_2.c。测试的标准也很简单,关键就是单位时间内跑了多少个dhrystone,比如说可以按照1s为计量单位。当然,测试的是时候需要你事先输入预计的dhrystone数目,可以慢慢试。一般说来,如果是简单的嵌入式cpu,几千到几万就可以;如果普通pc的cpu,则需要几百万到几千万不等。

    dhry.h文件如下,

[cpp] 
  1. /* 
  2.  **************************************************************************** 
  3.  * 
  4.  *                   "DHRYSTONE" Benchmark Program 
  5.  *                   ----------------------------- 
  6.  *                                                                             
  7.  *  Version:    C, Version 2.1 
  8.  *                                                                             
  9.  *  File:       dhry.h (part 1 of 3) 
  10.  * 
  11.  *  Date:       May 25, 1988 
  12.  * 
  13.  *  Author:     Reinhold P. Weicker 
  14.  *                      Siemens AG, E STE 35 
  15.  *                      Postfach 3240 
  16.  *                      8520 Erlangen 
  17.  *                      Germany (West) 
  18.  *                              Phone:  [xxx-49]-9131-7-20330 
  19.  *                                      (8-17 Central European Time) 
  20.  *                              Usenet: ..!mcvax!unido!estevax!weicker 
  21.  * 
  22.  *              Original Version (in Ada) published in 
  23.  *              "Communications of the ACM" vol. 27., no. 10 (Oct. 1984), 
  24.  *              pp. 1013 - 1030, together with the statistics 
  25.  *              on which the distribution of statements etc. is based. 
  26.  * 
  27.  *              In this C version, the following C library functions are used: 
  28.  *              - strcpy, strcmp (inside the measurement loop) 
  29.  *              - printf, scanf (outside the measurement loop) 
  30.  *              In addition, Berkeley UNIX system calls "times ()" or "time ()" 
  31.  *              are used for execution time measurement. For measurements 
  32.  *              on other systems, these calls have to be changed. 
  33.  * 
  34.  *  Collection of Results: 
  35.  *              Reinhold Weicker (address see above) and 
  36.  *               
  37.  *              Rick Richardson 
  38.  *              PC Research. Inc. 
  39.  *              94 Apple Orchard Drive 
  40.  *              Tinton Falls, NJ 07724 
  41.  *                      Phone:  (201) 389-8963 (9-17 EST)                
  42.  *                      Usenet: ...!uunet!pcrat!rick 
  43.  * 
  44.  *      Please send results to Rick Richardson and/or Reinhold Weicker. 
  45.  *      Complete information should be given on hardware and software used. 
  46.  *      Hardware information includes: Machine type, CPU, type and size 
  47.  *      of caches; for microprocessors: clock frequency, memory speed 
  48.  *      (number of wait states). 
  49.  *      Software information includes: Compiler (and runtime library) 
  50.  *      manufacturer and version, compilation switches, OS version. 
  51.  *      The Operating System version may give an indication about the 
  52.  *      compiler; Dhrystone itself performs no OS calls in the measurement loop. 
  53.  * 
  54.  *      The complete output generated by the program should be mailed 
  55.  *      such that at least some checks for correctness can be made. 
  56.  * 
  57.  *************************************************************************** 
  58.  * 
  59.  *  History:    This version C/2.1 has been made for two reasons: 
  60.  * 
  61.  *              1) There is an obvious need for a common C version of 
  62.  *              Dhrystone, since C is at present the most popular system 
  63.  *              programming language for the class of processors 
  64.  *              (microcomputers, minicomputers) where Dhrystone is used most. 
  65.  *              There should be, as far as possible, only one C version of 
  66.  *              Dhrystone such that results can be compared without 
  67.  *              restrictions. In the past, the C versions distributed 
  68.  *              by Rick Richardson (Version 1.1) and by Reinhold Weicker 
  69.  *              had small (though not significant) differences. 
  70.  * 
  71.  *              2) As far as it is possible without changes to the Dhrystone 
  72.  *              statistics, optimizing compilers should be prevented from 
  73.  *              removing significant statements. 
  74.  * 
  75.  *              This C version has been developed in cooperation with 
  76.  *              Rick Richardson (Tinton Falls, NJ), it incorporates many 
  77.  *              ideas from the "Version 1.1" distributed previously by 
  78.  *              him over the UNIX network Usenet. 
  79.  *              I also thank Chaim Benedelac (National Semiconductor), 
  80.  *              David Ditzel (SUN), Earl Killian and John Mashey (MIPS), 
  81.  *              Alan Smith and Rafael Saavedra-Barrera (UC at Berkeley) 
  82.  *              for their help with comments on earlier versions of the 
  83.  *              benchmark. 
  84.  * 
  85.  *  Changes:    In the initialization part, this version follows mostly 
  86.  *              Rick Richardson's version distributed via Usenet, not the 
  87.  *              version distributed earlier via floppy disk by Reinhold Weicker. 
  88.  *              As a concession to older compilers, names have been made 
  89.  *              unique within the first 8 characters. 
  90.  *              Inside the measurement loop, this version follows the 
  91.  *              version previously distributed by Reinhold Weicker. 
  92.  * 
  93.  *              At several places in the benchmark, code has been added, 
  94.  *              but within the measurement loop only in branches that  
  95.  *              are not executed. The intention is that optimizing compilers 
  96.  *              should be prevented from moving code out of the measurement 
  97.  *              loop, or from removing code altogether. Since the statements 
  98.  *              that are executed within the measurement loop have NOT been 
  99.  *              changed, the numbers defining the "Dhrystone distribution" 
  100.  *              (distribution of statements, operand types and locality) 
  101.  *              still hold. Except for sophisticated optimizing compilers, 
  102.  *              execution times for this version should be the same as 
  103.  *              for previous versions. 
  104.  *               
  105.  *              Since it has proven difficult to subtract the time for the 
  106.  *              measurement loop overhead in a correct way, the loop check 
  107.  *              has been made a part of the benchmark. This does have 
  108.  *              an impact - though a very minor one - on the distribution 
  109.  *              statistics which have been updated for this version. 
  110.  * 
  111.  *              All changes within the measurement loop are described 
  112.  *              and discussed in the companion paper "Rationale for 
  113.  *              Dhrystone version 2". 
  114.  * 
  115.  *              Because of the self-imposed limitation that the order and 
  116.  *              distribution of the executed statements should not be 
  117.  *              changed, there are still cases where optimizing compilers 
  118.  *              may not generate code for some statements. To a certain 
  119.  *              degree, this is unavoidable for small synthetic benchmarks. 
  120.  *              Users of the benchmark are advised to check code listings 
  121.  *              whether code is generated for all statements of Dhrystone. 
  122.  * 
  123.  *              Version 2.1 is identical to version 2.0 distributed via 
  124.  *              the UNIX network Usenet in March 1988 except that it corrects 
  125.  *              some minor deficiencies that were found by users of version 2.0. 
  126.  *              The only change within the measurement loop is that a 
  127.  *              non-executed "else" part was added to the "if" statement in 
  128.  *              Func_3, and a non-executed "else" part removed from Proc_3. 
  129.  * 
  130.  *************************************************************************** 
  131.  * 
  132.  * Defines:     The following "Defines" are possible: 
  133.  *              -DREG=register          (default: Not defined) 
  134.  *                      As an approximation to what an average C programmer 
  135.  *                      might do, the "register" storage class is applied 
  136.  *                      (if enabled by -DREG=register) 
  137.  *                      - for local variables, if they are used (dynamically) 
  138.  *                        five or more times 
  139.  *                      - for parameters if they are used (dynamically) 
  140.  *                        six or more times 
  141.  *                      Note that an optimal "register" strategy is 
  142.  *                      compiler-dependent, and that "register" declarations 
  143.  *                      do not necessarily lead to faster execution. 
  144.  *              -DNOSTRUCTASSIGN        (default: Not defined) 
  145.  *                      Define if the C compiler does not support 
  146.  *                      assignment of structures. 
  147.  *              -DNOENUMS               (default: Not defined) 
  148.  *                      Define if the C compiler does not support 
  149.  *                      enumeration types. 
  150.  *              -DTIMES                 (default) 
  151.  *              -DTIME 
  152.  *                      The "times" function of UNIX (returning process times) 
  153.  *                      or the "time" function (returning wallclock time) 
  154.  *                      is used for measurement.  
  155.  *                      For single user machines, "time ()" is adequate. For 
  156.  *                      multi-user machines where you cannot get single-user 
  157.  *                      access, use the "times ()" function. If you have 
  158.  *                      neither, use a stopwatch in the dead of night. 
  159.  *                      "printf"s are provided marking the points "Start Timer" 
  160.  *                      and "Stop Timer". DO NOT use the UNIX "time(1)" 
  161.  *                      command, as this will measure the total time to 
  162.  *                      run this program, which will (erroneously) include 
  163.  *                      the time to allocate storage (malloc) and to perform 
  164.  *                      the initialization. 
  165.  *              -DHZ=nnn 
  166.  *                      In Berkeley UNIX, the function "times" returns process 
  167.  *                      time in 1/HZ seconds, with HZ = 60 for most systems. 
  168.  *                      CHECK YOUR SYSTEM DESCRIPTION BEFORE YOU JUST APPLY 
  169.  *                      A VALUE. 
  170.  * 
  171.  *************************************************************************** 
  172.  * 
  173.  *  Compilation model and measurement (IMPORTANT): 
  174.  * 
  175.  *  This C version of Dhrystone consists of three files: 
  176.  *  - dhry.h (this file, containing global definitions and comments) 
  177.  *  - dhry_1.c (containing the code corresponding to Ada package Pack_1) 
  178.  *  - dhry_2.c (containing the code corresponding to Ada package Pack_2) 
  179.  * 
  180.  *  The following "ground rules" apply for measurements: 
  181.  *  - Separate compilation 
  182.  *  - No procedure merging 
  183.  *  - Otherwise, compiler optimizations are allowed but should be indicated 
  184.  *  - Default results are those without register declarations 
  185.  *  See the companion paper "Rationale for Dhrystone Version 2" for a more 
  186.  *  detailed discussion of these ground rules. 
  187.  * 
  188.  *  For 16-Bit processors (e.g. 80186, 80286), times for all compilation 
  189.  *  models ("small", "medium", "large" etc.) should be given if possible, 
  190.  *  together with a definition of these models for the compiler system used. 
  191.  * 
  192.  ************************************************************************** 
  193.  * 
  194.  *  Dhrystone (C version) statistics: 
  195.  * 
  196.  *  [Comment from the first distribution, updated for version 2. 
  197.  *   Note that because of language differences, the numbers are slightly 
  198.  *   different from the Ada version.] 
  199.  * 
  200.  *  The following program contains statements of a high level programming 
  201.  *  language (here: C) in a distribution considered representative:            
  202.  * 
  203.  *    assignments                  52 (51.0 %) 
  204.  *    control statements           33 (32.4 %) 
  205.  *    procedure, function calls    17 (16.7 %) 
  206.  * 
  207.  *  103 statements are dynamically executed. The program is balanced with 
  208.  *  respect to the three aspects:                                              
  209.  * 
  210.  *    - statement type 
  211.  *    - operand type 
  212.  *    - operand locality 
  213.  *         operand global, local, parameter, or constant.                      
  214.  * 
  215.  *  The combination of these three aspects is balanced only approximately.     
  216.  * 
  217.  *  1. Statement Type:                                                         
  218.  *  -----------------             number 
  219.  * 
  220.  *     V1 = V2                     9 
  221.  *       (incl. V1 = F(..) 
  222.  *     V = Constant               12 
  223.  *     Assignment,                 7 
  224.  *       with array element 
  225.  *     Assignment,                 6 
  226.  *       with record component 
  227.  *                                -- 
  228.  *                                34       34 
  229.  * 
  230.  *     X = Y +|-|"&&"|"|" Z        5 
  231.  *     X = Y +|-|"==" Constant     6 
  232.  *     X = X +|- 1                 3 
  233.  *     X = Y *|/ Z                 2 
  234.  *     X = Expression,             1 
  235.  *           two operators 
  236.  *     X = Expression,             1 
  237.  *           three operators 
  238.  *                                -- 
  239.  *                                18       18 
  240.  * 
  241.  *     if ....                    14 
  242.  *       with "else"      7 
  243.  *       without "else"   7 
  244.  *           executed        3 
  245.  *           not executed    4 
  246.  *     for ...                     7  |  counted every time 
  247.  *     while ...                   4  |  the loop condition 
  248.  *     do ... while                1  |  is evaluated 
  249.  *     switch ...                  1 
  250.  *     break                       1 
  251.  *     declaration with            1 
  252.  *       initialization 
  253.  *                                -- 
  254.  *                                34       34 
  255.  * 
  256.  *     P (...)  procedure call    11 
  257.  *       user procedure      10 
  258.  *       library procedure    1 
  259.  *     X = F (...) 
  260.  *             function  call      6 
  261.  *       user function        5                                          
  262.  *       library function     1                                                
  263.  *                                --                                           
  264.  *                                17       17 
  265.  *                                        --- 
  266.  *                                        103 
  267.  * 
  268.  *    The average number of parameters in procedure or function calls 
  269.  *    is 1.82 (not counting the function values aX * 
  270.  * 
  271.  *  2. Operators 
  272.  *  ------------ 
  273.  *                          number    approximate 
  274.  *                                    percentage 
  275.  * 
  276.  *    Arithmetic             32          50.8                                  
  277.  * 
  278.  *       +                     21          33.3                               
  279.  *       -                      7          11.1                               
  280.  *       *                      3           4.8 
  281.  *       / (int div)            1           1.6 
  282.  * 
  283.  *    Comparison             27           42.8 
  284.  * 
  285.  *       ==                     9           14.3 
  286.  *       /=                     4            6.3 
  287.  *       >                      1            1.6 
  288.  *       <                      3            4.8 
  289.  *       >=                     1            1.6 
  290.  *       <=                     9           14.3 
  291.  * 
  292.  *    Logic                   4            6.3 
  293.  * 
  294.  *       && (AND-THEN)          1            1.6 
  295.  *       |  (OR)                1            1.6 
  296.  *       !  (NOT)               2            3.2 
  297.  *  
  298.  *                           --          ----- 
  299.  *                           63          100.1 
  300.  * 
  301.  * 
  302.  *  3. Operand Type (counted once per operand reference): 
  303.  *  --------------- 
  304.  *                          number    approximate 
  305.  *                                    percentage 
  306.  * 
  307.  *     Integer               175        72.3 % 
  308.  *     Character              45        18.6 % 
  309.  *     Pointer                12         5.0 % 
  310.  *     String30                6         2.5 % 
  311.  *     Array                   2         0.8 % 
  312.  *     Record                  2         0.8 % 
  313.  *                           ---       ------- 
  314.  *                           242       100.0 % 
  315.  * 
  316.  *  When there is an access path leading to the final operand (e.g. a record 
  317.  *  component), only the final data type on the access path is counted.        
  318.  * 
  319.  * 
  320.  *  4. Operand Locality:                                                       
  321.  *  ------------------- 
  322.  *                                number    approximate 
  323.  *                                          percentage 
  324.  * 
  325.  *     local variable              114        47.1 % 
  326.  *     global variable              22         9.1 % 
  327.  *     parameter                    45        18.6 % 
  328.  *        value                        23         9.5 % 
  329.  *        reference                    22         9.1 % 
  330.  *     function result               6         2.5 % 
  331.  *     constant                     55        22.7 % 
  332.  *                                 ---       ------- 
  333.  *                                 242       100.0 % 
  334.  * 
  335.  * 
  336.  *  The program does not compute anything meaningful, but it is syntactically 
  337.  *  and semantically correct. All variables have a value assigned to them 
  338.  *  before they are used as a source operand. 
  339.  * 
  340.  *  There has been no explicit effort to account for the effects of a 
  341.  *  cache, or to balance the use of long or short displacements for code or 
  342.  *  data. 
  343.  * 
  344.  *************************************************************************** 
  345.  */  
  346.   
  347. /* Compiler and system dependent definitions: */  
  348.   
  349. #define TIME  
  350.   
  351. #ifndef TIME  
  352. #undef TIMES  
  353. #define TIMES  
  354. #endif  
  355.                 /* Use times(2) time function unless    */  
  356.                 /* explicitly defined otherwise         */  
  357.   
  358. #ifdef MSC_CLOCK  
  359. #undef HZ  
  360. #undef TIMES  
  361. #include <time.h>  
  362. #define HZ  CLK_TCK  
  363. #endif  
  364.         /* Use Microsoft C hi-res clock */  
  365.   
  366. #ifdef TIMES  
  367. #include <sys/types.h>  
  368. #include <sys/times.h>  
  369.                 /* for "times" */  
  370. #endif  
  371.   
  372. #define Mic_secs_Per_Second     1000000.0  
  373.                 /* Berkeley UNIX C returns process times in seconds/HZ */  
  374.   
  375. #ifdef  NOSTRUCTASSIGN  
  376. #define structassign(d, s)      memcpy(&(d), &(s), sizeof(d))  
  377. #else  
  378. #define structassign(d, s)      d = s  
  379. #endif  
  380.   
  381. #ifdef  NOENUM  
  382. #define Ident_1 0  
  383. #define Ident_2 1  
  384. #define Ident_3 2  
  385. #define Ident_4 3  
  386. #define Ident_5 4  
  387.   typedef int   Enumeration;  
  388. #else  
  389.   typedef       enum    {Ident_1, Ident_2, Ident_3, Ident_4, Ident_5}  
  390.                 Enumeration;  
  391. #endif  
  392.         /* for boolean and enumeration types in Ada, Pascal */  
  393.   
  394. /* General definitions: */  
  395.   
  396. #include <stdio.h>  
  397.                 /* for strcpy, strcmp */  
  398.   
  399. #define Null 0   
  400.                 /* Value of a Null pointer */  
  401. #define true  1  
  402. #define false 0  
  403.   
  404. typedef int     One_Thirty;  
  405. typedef int     One_Fifty;  
  406. typedef char    Capital_Letter;  
  407. typedef int     Boolean;  
  408. typedef char    Str_30 [31];  
  409. typedef int     Arr_1_Dim [50];  
  410. typedef int     Arr_2_Dim [50] [50];  
  411.   
  412. typedef struct record   
  413.     {  
  414.     struct record *Ptr_Comp;  
  415.     Enumeration    Discr;  
  416.     union {  
  417.           struct {  
  418.                   Enumeration Enum_Comp;  
  419.                   int         Int_Comp;  
  420.                   char        Str_Comp [31];  
  421.                   } var_1;  
  422.           struct {  
  423.                   Enumeration E_Comp_2;  
  424.                   char        Str_2_Comp [31];  
  425.                   } var_2;  
  426.           struct {  
  427.                   char        Ch_1_Comp;  
  428.                   char        Ch_2_Comp;  
  429.                   } var_3;  
  430.           } variant;  
  431.       } Rec_Type, *Rec_Pointer;  

    dhry_1.c如下所示,

[cpp] 
  1. /* 
  2.  **************************************************************************** 
  3.  * 
  4.  *                   "DHRYSTONE" Benchmark Program 
  5.  *                   ----------------------------- 
  6.  *                                                                             
  7.  *  Version:    C, Version 2.1 
  8.  *                                                                             
  9.  *  File:       dhry_1.c (part 2 of 3) 
  10.  * 
  11.  *  Date:       May 25, 1988 
  12.  * 
  13.  *  Author:     Reinhold P. Weicker 
  14.  * 
  15.  **************************************************************************** 
  16.  */  
  17.   
  18. #include "dhry.h"  
  19.   
  20. /* Global Variables: */  
  21.   
  22. Rec_Pointer     Ptr_Glob,  
  23.                 Next_Ptr_Glob;  
  24. int             Int_Glob;  
  25. Boolean         Bool_Glob;  
  26. char            Ch_1_Glob,  
  27.                 Ch_2_Glob;  
  28. int             Arr_1_Glob [50];  
  29. int             Arr_2_Glob [50] [50];  
  30.   
  31. extern char     *malloc ();  
  32. Enumeration     Func_1 ();  
  33.   /* forward declaration necessary since Enumeration may not simply be int */  
  34.   
  35. #ifndef REG  
  36.         Boolean Reg = false;  
  37. #define REG  
  38.         /* REG becomes defined as empty */  
  39.         /* i.e. no register variables   */  
  40. #else  
  41.         Boolean Reg = true;  
  42. #endif  
  43.   
  44. /* variables for time measurement: */  
  45.   
  46. #ifdef TIMES  
  47. struct tms      time_info;  
  48. extern  int     times ();  
  49.                 /* see library function "times" */  
  50. #define Too_Small_Time (2*HZ)  
  51.                 /* Measurements should last at least about 2 seconds */  
  52. #endif  
  53. #ifdef TIME  
  54. extern long     time();  
  55.                 /* see library function "time"  */  
  56. #define Too_Small_Time 2  
  57.                 /* Measurements should last at least 2 seconds */  
  58. #endif  
  59. #ifdef MSC_CLOCK  
  60. extern clock_t  clock();  
  61. #define Too_Small_Time (2*HZ)  
  62. #endif  
  63.   
  64. long            Begin_Time,  
  65.                 End_Time,  
  66.                 User_Time;  
  67. float           Microseconds,  
  68.                 Dhrystones_Per_Second;  
  69.   
  70. /* end of variables for time measurement */  
  71.   
  72.   
  73. main ()  
  74. /*****/  
  75.   
  76.   /* main program, corresponds to procedures        */  
  77.   /* Main and Proc_0 in the Ada version             */  
  78. {  
  79.         One_Fifty       Int_1_Loc;  
  80.   REG   One_Fifty       Int_2_Loc;  
  81.         One_Fifty       Int_3_Loc;  
  82.   REG   char            Ch_Index;  
  83.         Enumeration     Enum_Loc;  
  84.         Str_30          Str_1_Loc;  
  85.         Str_30          Str_2_Loc;  
  86.   REG   int             Run_Index;  
  87.   REG   int             Number_Of_Runs;  
  88.   
  89.   /* Initializations */  
  90.   
  91.   Next_Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));  
  92.   Ptr_Glob = (Rec_Pointer) malloc (sizeof (Rec_Type));  
  93.   
  94.   Ptr_Glob->Ptr_Comp                    = Next_Ptr_Glob;  
  95.   Ptr_Glob->Discr                       = Ident_1;  
  96.   Ptr_Glob->variant.var_1.Enum_Comp     = Ident_3;  
  97.   Ptr_Glob->variant.var_1.Int_Comp      = 40;  
  98.   strcpy (Ptr_Glob->variant.var_1.Str_Comp,   
  99.           "DHRYSTONE PROGRAM, SOME STRING");  
  100.   strcpy (Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");  
  101.   
  102.   Arr_2_Glob [8][7] = 10;  
  103.         /* Was missing in published program. Without this statement,    */  
  104.         /* Arr_2_Glob [8][7] would have an undefined value.             */  
  105.         /* Warning: With 16-Bit processors and Number_Of_Runs > 32000,  */  
  106.         /* overflow may occur for this array element.                   */  
  107.   
  108.   printf ("\n");  
  109.   printf ("Dhrystone Benchmark, Version 2.1 (Language: C)\n");  
  110.   printf ("\n");  
  111.   if (Reg)  
  112.   {  
  113.     printf ("Program compiled with 'register' attribute\n");  
  114.     printf ("\n");  
  115.   }  
  116.   else  
  117.   {  
  118.     printf ("Program compiled without 'register' attribute\n");  
  119.     printf ("\n");  
  120.   }  
  121.   printf ("Please give the number of runs through the benchmark: ");  
  122.   {  
  123.     int n;  
  124.     scanf ("%d", &n);  
  125.     Number_Of_Runs = n;  
  126.   }  
  127.   printf ("\n");  
  128.   
  129.   printf ("Execution starts, %d runs through Dhrystone\n", Number_Of_Runs);  
  130.   
  131.   /***************/  
  132.   /* Start timer */  
  133.   /***************/  
  134.    
  135. #ifdef TIMES  
  136.   times (&time_info);  
  137.   Begin_Time = (long) time_info.tms_utime;  
  138. #endif  
  139. #ifdef TIME  
  140.   Begin_Time = time ( (long *) 0);  
  141. #endif  
  142. #ifdef MSC_CLOCK  
  143.   Begin_Time = clock();  
  144. #endif  
  145.   
  146.   for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index)  
  147.   {  
  148.   
  149.     Proc_5();  
  150.     Proc_4();  
  151.       /* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */  
  152.     Int_1_Loc = 2;  
  153.     Int_2_Loc = 3;  
  154.     strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");  
  155.     Enum_Loc = Ident_2;  
  156.     Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);  
  157.       /* Bool_Glob == 1 */  
  158.     while (Int_1_Loc < Int_2_Loc)  /* loop body executed once */  
  159.     {  
  160.       Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;  
  161.         /* Int_3_Loc == 7 */  
  162.       Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);  
  163.         /* Int_3_Loc == 7 */  
  164.       Int_1_Loc += 1;  
  165.     } /* while */  
  166.       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */  
  167.     Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);  
  168.       /* Int_Glob == 5 */  
  169.     Proc_1 (Ptr_Glob);  
  170.     for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index)  
  171.                              /* loop body executed twice */  
  172.     {  
  173.       if (Enum_Loc == Func_1 (Ch_Index, 'C'))  
  174.           /* then, not executed */  
  175.         {  
  176.         Proc_6 (Ident_1, &Enum_Loc);  
  177.         strcpy (Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");  
  178.         Int_2_Loc = Run_Index;  
  179.         Int_Glob = Run_Index;  
  180.         }  
  181.     }  
  182.       /* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */  
  183.     Int_2_Loc = Int_2_Loc * Int_1_Loc;  
  184.     Int_1_Loc = Int_2_Loc / Int_3_Loc;  
  185.     Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;  
  186.       /* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */  
  187.     Proc_2 (&Int_1_Loc);  
  188.       /* Int_1_Loc == 5 */  
  189.   
  190.   } /* loop "for Run_Index" */  
  191.   
  192.   /**************/  
  193.   /* Stop timer */  
  194.   /**************/  
  195.     
  196. #ifdef TIMES  
  197.   times (&time_info);  
  198.   End_Time = (long) time_info.tms_utime;  
  199. #endif  
  200. #ifdef TIME  
  201.   End_Time = time ( (long *) 0);  
  202. #endif  
  203. #ifdef MSC_CLOCK  
  204.   End_Time = clock();  
  205. #endif  
  206.   
  207.   printf ("Execution ends\n");  
  208.   printf ("\n");  
  209.   printf ("Final values of the variables used in the benchmark:\n");  
  210.   printf ("\n");  
  211.   printf ("Int_Glob:            %d\n", Int_Glob);  
  212.   printf ("        should be:   %d\n", 5);  
  213.   printf ("Bool_Glob:           %d\n", Bool_Glob);  
  214.   printf ("        should be:   %d\n", 1);  
  215.   printf ("Ch_1_Glob:           %c\n", Ch_1_Glob);  
  216.   printf ("        should be:   %c\n"'A');  
  217.   printf ("Ch_2_Glob:           %c\n", Ch_2_Glob);  
  218.   printf ("        should be:   %c\n"'B');  
  219.   printf ("Arr_1_Glob[8]:       %d\n", Arr_1_Glob[8]);  
  220.   printf ("        should be:   %d\n", 7);  
  221.   printf ("Arr_2_Glob[8][7]:    %d\n", Arr_2_Glob[8][7]);  
  222.   printf ("        should be:   Number_Of_Runs + 10\n");  
  223.   printf ("Ptr_Glob->\n");  
  224.   printf ("  Ptr_Comp:          %d\n", (int) Ptr_Glob->Ptr_Comp);  
  225.   printf ("        should be:   (implementation-dependent)\n");  
  226.   printf ("  Discr:             %d\n", Ptr_Glob->Discr);  
  227.   printf ("        should be:   %d\n", 0);  
  228.   printf ("  Enum_Comp:         %d\n", Ptr_Glob->variant.var_1.Enum_Comp);  
  229.   printf ("        should be:   %d\n", 2);  
  230.   printf ("  Int_Comp:          %d\n", Ptr_Glob->variant.var_1.Int_Comp);  
  231.   printf ("        should be:   %d\n", 17);  
  232.   printf ("  Str_Comp:          %s\n", Ptr_Glob->variant.var_1.Str_Comp);  
  233.   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");  
  234.   printf ("Next_Ptr_Glob->\n");  
  235.   printf ("  Ptr_Comp:          %d\n", (int) Next_Ptr_Glob->Ptr_Comp);  
  236.   printf ("        should be:   (implementation-dependent), same as above\n");  
  237.   printf ("  Discr:             %d\n", Next_Ptr_Glob->Discr);  
  238.   printf ("        should be:   %d\n", 0);  
  239.   printf ("  Enum_Comp:         %d\n", Next_Ptr_Glob->variant.var_1.Enum_Comp);  
  240.   printf ("        should be:   %d\n", 1);  
  241.   printf ("  Int_Comp:          %d\n", Next_Ptr_Glob->variant.var_1.Int_Comp);  
  242.   printf ("        should be:   %d\n", 18);  
  243.   printf ("  Str_Comp:          %s\n",  
  244.                                 Next_Ptr_Glob->variant.var_1.Str_Comp);  
  245.   printf ("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");  
  246.   printf ("Int_1_Loc:           %d\n", Int_1_Loc);  
  247.   printf ("        should be:   %d\n", 5);  
  248.   printf ("Int_2_Loc:           %d\n", Int_2_Loc);  
  249.   printf ("        should be:   %d\n", 13);  
  250.   printf ("Int_3_Loc:           %d\n", Int_3_Loc);  
  251.   printf ("        should be:   %d\n", 7);  
  252.   printf ("Enum_Loc:            %d\n", Enum_Loc);  
  253.   printf ("        should be:   %d\n", 1);  
  254.   printf ("Str_1_Loc:           %s\n", Str_1_Loc);  
  255.   printf ("        should be:   DHRYSTONE PROGRAM, 1'ST STRING\n");  
  256.   printf ("Str_2_Loc:           %s\n", Str_2_Loc);  
  257.   printf ("        should be:   DHRYSTONE PROGRAM, 2'ND STRING\n");  
  258.   printf ("\n");  
  259.   
  260.   User_Time = End_Time - Begin_Time;  
  261.   
  262.   if (User_Time < Too_Small_Time)  
  263.   {  
  264.     printf ("Measured time too small to obtain meaningful results\n");  
  265.     printf ("Please increase number of runs\n");  
  266.     printf ("\n");  
  267.   }  
  268.   else  
  269.   {  
  270. #ifdef TIME  
  271.     Microseconds = (float) User_Time * Mic_secs_Per_Second   
  272.                         / (float) Number_Of_Runs;  
  273.     Dhrystones_Per_Second = (float) Number_Of_Runs / (float) User_Time;  
  274. #else  
  275.     Microseconds = (float) User_Time * Mic_secs_Per_Second   
  276.                         / ((float) HZ * ((float) Number_Of_Runs));  
  277.     Dhrystones_Per_Second = ((float) HZ * (float) Number_Of_Runs)  
  278.                         / (float) User_Time;  
  279. #endif  
  280.     printf ("Microseconds for one run through Dhrystone: ");  
  281.     printf ("%6.1f \n", Microseconds);  
  282.     printf ("Dhrystones per Second:                      ");  
  283.     printf ("%6.1f \n", Dhrystones_Per_Second);  
  284.     printf ("\n");  
  285.   }  
  286.     
  287. }  
  288.   
  289.   
  290. Proc_1 (Ptr_Val_Par)  
  291. /******************/  
  292.   
  293. REG Rec_Pointer Ptr_Val_Par;  
  294.     /* executed once */  
  295. {  
  296.   REG Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;    
  297.                                         /* == Ptr_Glob_Next */  
  298.   /* Local variable, initialized with Ptr_Val_Par->Ptr_Comp,    */  
  299.   /* corresponds to "rename" in Ada, "with" in Pascal           */  
  300.     
  301.   structassign (*Ptr_Val_Par->Ptr_Comp, *Ptr_Glob);   
  302.   Ptr_Val_Par->variant.var_1.Int_Comp = 5;  
  303.   Next_Record->variant.var_1.Int_Comp   
  304.         = Ptr_Val_Par->variant.var_1.Int_Comp;  
  305.   Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;  
  306.   Proc_3 (&Next_Record->Ptr_Comp);  
  307.     /* Ptr_Val_Par->Ptr_Comp->Ptr_Comp  
  308.                         == Ptr_Glob->Ptr_Comp */  
  309.   if (Next_Record->Discr == Ident_1)  
  310.     /* then, executed */  
  311.   {  
  312.     Next_Record->variant.var_1.Int_Comp = 6;  
  313.     Proc_6 (Ptr_Val_Par->variant.var_1.Enum_Comp,   
  314.            &Next_Record->variant.var_1.Enum_Comp);  
  315.     Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;  
  316.     Proc_7 (Next_Record->variant.var_1.Int_Comp, 10,   
  317.            &Next_Record->variant.var_1.Int_Comp);  
  318.   }  
  319.   else /* not executed */  
  320.     structassign (*Ptr_Val_Par, *Ptr_Val_Par->Ptr_Comp);  
  321. /* Proc_1 */  
  322.   
  323.   
  324. Proc_2 (Int_Par_Ref)  
  325. /******************/  
  326.     /* executed once */  
  327.     /* *Int_Par_Ref == 1, becomes 4 */  
  328.   
  329. One_Fifty   *Int_Par_Ref;  
  330. {  
  331.   One_Fifty  Int_Loc;    
  332.   Enumeration   Enum_Loc;  
  333.   
  334.   Int_Loc = *Int_Par_Ref + 10;  
  335.   do /* executed once */  
  336.     if (Ch_1_Glob == 'A')  
  337.       /* then, executed */  
  338.     {  
  339.       Int_Loc -= 1;  
  340.       *Int_Par_Ref = Int_Loc - Int_Glob;  
  341.       Enum_Loc = Ident_1;  
  342.     } /* if */  
  343.   while (Enum_Loc != Ident_1); /* true */  
  344. /* Proc_2 */  
  345.   
  346.   
  347. Proc_3 (Ptr_Ref_Par)  
  348. /******************/  
  349.     /* executed once */  
  350.     /* Ptr_Ref_Par becomes Ptr_Glob */  
  351.   
  352. Rec_Pointer *Ptr_Ref_Par;  
  353.   
  354. {  
  355.   if (Ptr_Glob != Null)  
  356.     /* then, executed */  
  357.     *Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;  
  358.   Proc_7 (10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);  
  359. /* Proc_3 */  
  360.   
  361.   
  362. Proc_4 () /* without parameters */  
  363. /*******/  
  364.     /* executed once */  
  365. {  
  366.   Boolean Bool_Loc;  
  367.   
  368.   Bool_Loc = Ch_1_Glob == 'A';  
  369.   Bool_Glob = Bool_Loc | Bool_Glob;  
  370.   Ch_2_Glob = 'B';  
  371. /* Proc_4 */  
  372.   
  373.   
  374. Proc_5 () /* without parameters */  
  375. /*******/  
  376.     /* executed once */  
  377. {  
  378.   Ch_1_Glob = 'A';  
  379.   Bool_Glob = false;  
  380. /* Proc_5 */  
  381.   
  382.   
  383.         /* Procedure for the assignment of structures,          */  
  384.         /* if the C compiler doesn't support this feature       */  
  385. #ifdef  NOSTRUCTASSIGN  
  386. memcpy (d, s, l)  
  387. register char   *d;  
  388. register char   *s;  
  389. register int    l;  
  390. {  
  391.         while (l--) *d++ = *s++;  
  392. }  
  393. #endif  

    dhry_2.c文件如下,

[cpp] 
  1. /* 
  2.  **************************************************************************** 
  3.  * 
  4.  *                   "DHRYSTONE" Benchmark Program 
  5.  *                   ----------------------------- 
  6.  *                                                                             
  7.  *  Version:    C, Version 2.1 
  8.  *                                                                             
  9.  *  File:       dhry_2.c (part 3 of 3) 
  10.  * 
  11.  *  Date:       May 25, 1988 
  12.  * 
  13.  *  Author:     Reinhold P. Weicker 
  14.  * 
  15.  **************************************************************************** 
  16.  */  
  17.   
  18. #include "dhry.h"  
  19.   
  20. #ifndef REG  
  21. #define REG  
  22.         /* REG becomes defined as empty */  
  23.         /* i.e. no register variables   */  
  24. #endif  
  25.   
  26. extern  int     Int_Glob;  
  27. extern  char    Ch_1_Glob;  
  28.   
  29.   
  30. Proc_6 (Enum_Val_Par, Enum_Ref_Par)  
  31. /*********************************/  
  32.     /* executed once */  
  33.     /* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */  
  34.   
  35. Enumeration  Enum_Val_Par;  
  36. Enumeration *Enum_Ref_Par;  
  37. {  
  38.   *Enum_Ref_Par = Enum_Val_Par;  
  39.   if (! Func_3 (Enum_Val_Par))  
  40.     /* then, not executed */  
  41.     *Enum_Ref_Par = Ident_4;  
  42.   switch (Enum_Val_Par)  
  43.   {  
  44.     case Ident_1:   
  45.       *Enum_Ref_Par = Ident_1;  
  46.       break;  
  47.     case Ident_2:   
  48.       if (Int_Glob > 100)  
  49.         /* then */  
  50.       *Enum_Ref_Par = Ident_1;  
  51.       else *Enum_Ref_Par = Ident_4;  
  52.       break;  
  53.     case Ident_3: /* executed */  
  54.       *Enum_Ref_Par = Ident_2;  
  55.       break;  
  56.     case Ident_4: break;  
  57.     case Ident_5:   
  58.       *Enum_Ref_Par = Ident_3;  
  59.       break;  
  60.   } /* switch */  
  61. /* Proc_6 */  
  62.   
  63.   
  64. Proc_7 (Int_1_Par_Val, Int_2_Par_Val, Int_Par_Ref)  
  65. /**********************************************/  
  66.     /* executed three times                                      */   
  67.     /* first call:      Int_1_Par_Val == 2, Int_2_Par_Val == 3,  */  
  68.     /*                  Int_Par_Ref becomes 7                    */  
  69.     /* second call:     Int_1_Par_Val == 10, Int_2_Par_Val == 5, */  
  70.     /*                  Int_Par_Ref becomes 17                   */  
  71.     /* third call:      Int_1_Par_Val == 6, Int_2_Par_Val == 10, */  
  72.     /*                  Int_Par_Ref becomes 18                   */  
  73. One_Fifty       Int_1_Par_Val;  
  74. One_Fifty       Int_2_Par_Val;  
  75. One_Fifty      *Int_Par_Ref;  
  76. {  
  77.   One_Fifty Int_Loc;  
  78.   
  79.   Int_Loc = Int_1_Par_Val + 2;  
  80.   *Int_Par_Ref = Int_2_Par_Val + Int_Loc;  
  81. /* Proc_7 */  
  82.   
  83.   
  84. Proc_8 (Arr_1_Par_Ref, Arr_2_Par_Ref, Int_1_Par_Val, Int_2_Par_Val)  
  85. /*********************************************************************/  
  86.     /* executed once      */  
  87.     /* Int_Par_Val_1 == 3 */  
  88.     /* Int_Par_Val_2 == 7 */  
  89. Arr_1_Dim       Arr_1_Par_Ref;  
  90. Arr_2_Dim       Arr_2_Par_Ref;  
  91. int             Int_1_Par_Val;  
  92. int             Int_2_Par_Val;  
  93. {  
  94.   REG One_Fifty Int_Index;  
  95.   REG One_Fifty Int_Loc;  
  96.   
  97.   Int_Loc = Int_1_Par_Val + 5;  
  98.   Arr_1_Par_Ref [Int_Loc] = Int_2_Par_Val;  
  99.   Arr_1_Par_Ref [Int_Loc+1] = Arr_1_Par_Ref [Int_Loc];  
  100.   Arr_1_Par_Ref [Int_Loc+30] = Int_Loc;  
  101.   for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index)  
  102.     Arr_2_Par_Ref [Int_Loc] [Int_Index] = Int_Loc;  
  103.   Arr_2_Par_Ref [Int_Loc] [Int_Loc-1] += 1;  
  104.   Arr_2_Par_Ref [Int_Loc+20] [Int_Loc] = Arr_1_Par_Ref [Int_Loc];  
  105.   Int_Glob = 5;  
  106. /* Proc_8 */  
  107.   
  108.   
  109. Enumeration Func_1 (Ch_1_Par_Val, Ch_2_Par_Val)  
  110. /*************************************************/  
  111.     /* executed three times                                         */  
  112.     /* first call:      Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R'    */  
  113.     /* second call:     Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C'    */  
  114.     /* third call:      Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C'    */  
  115.   
  116. Capital_Letter   Ch_1_Par_Val;  
  117. Capital_Letter   Ch_2_Par_Val;  
  118. {  
  119.   Capital_Letter        Ch_1_Loc;  
  120.   Capital_Letter        Ch_2_Loc;  
  121.   
  122.   Ch_1_Loc = Ch_1_Par_Val;  
  123.   Ch_2_Loc = Ch_1_Loc;  
  124.   if (Ch_2_Loc != Ch_2_Par_Val)  
  125.     /* then, executed */  
  126.     return (Ident_1);  
  127.   else  /* not executed */  
  128.   {  
  129.     Ch_1_Glob = Ch_1_Loc;  
  130.     return (Ident_2);  
  131.    }  
  132. /* Func_1 */  
  133.   
  134.   
  135. Boolean Func_2 (Str_1_Par_Ref, Str_2_Par_Ref)  
  136. /*************************************************/  
  137.     /* executed once */  
  138.     /* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */  
  139.     /* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */  
  140.   
  141. Str_30  Str_1_Par_Ref;  
  142. Str_30  Str_2_Par_Ref;  
  143. {  
  144.   REG One_Thirty        Int_Loc;  
  145.       Capital_Letter    Ch_Loc;  
  146.   
  147.   Int_Loc = 2;  
  148.   while (Int_Loc <= 2) /* loop body executed once */  
  149.     if (Func_1 (Str_1_Par_Ref[Int_Loc],  
  150.                 Str_2_Par_Ref[Int_Loc+1]) == Ident_1)  
  151.       /* then, executed */  
  152.     {  
  153.       Ch_Loc = 'A';  
  154.       Int_Loc += 1;  
  155.     } /* if, while */  
  156.   if (Ch_Loc >= 'W' && Ch_Loc < 'Z')  
  157.     /* then, not executed */  
  158.     Int_Loc = 7;  
  159.   if (Ch_Loc == 'R')  
  160.     /* then, not executed */  
  161.     return (true);  
  162.   else /* executed */  
  163.   {  
  164.     if (strcmp (Str_1_Par_Ref, Str_2_Par_Ref) > 0)  
  165.       /* then, not executed */  
  166.     {  
  167.       Int_Loc += 7;  
  168.       Int_Glob = Int_Loc;  
  169.       return (true);  
  170.     }  
  171.     else /* executed */  
  172.       return (false);  
  173.   } /* if Ch_Loc */  
  174. /* Func_2 */  
  175.   
  176.   
  177. Boolean Func_3 (Enum_Par_Val)  
  178. /***************************/  
  179.     /* executed once        */  
  180.     /* Enum_Par_Val == Ident_3 */  
  181. Enumeration Enum_Par_Val;  
  182. {  
  183.   Enumeration Enum_Loc;  
  184.   
  185.   Enum_Loc = Enum_Par_Val;  
  186.   if (Enum_Loc == Ident_3)  
  187.     /* then, executed */  
  188.     return (true);  
  189.   else /* not executed */  
  190.     return (false);  
  191. /* Func_3 */  

    当然,编译也很简单。如果是在linux上面,直接编写一个三个文件的Makefile即可;如果是在vc上面,把三个文件直接加入到vc工程里面,F7一把即可。有兴趣的朋友可以快点把这块代码拷贝一下,赶快测试一下。

转载地址:https://blog.csdn.net/yyyyyyyyyywwwwwwwwww/article/details/50481472 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Linux内存压力测试-memtester工具简析
下一篇:垃圾回收器

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月14日 05时48分47秒