1 //------------------------------------------------------------------------------
2 // GraphBLAS/Demo/Program/simple_demo.c: tests simple_rand and simple_timer
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 /*
11     A simple test that illustrates the use of simple_rand and simple_tic/toc.
12     This test does not require ANSI C11, nor GraphBLAS.  It only tests the
13     simple_* Demo functions.  The output of this test should look like the
14     following.  The random numbers should be the same.  On a Mac OSX system:
15 
16         time to call simple_tic 1 million times: 0.367798
17         time to generate 10 million random numbers: 0.10633
18         first 10 random numbers:
19             0.513871
20             0.175726
21             0.308634
22             0.534532
23             0.947630
24             0.171728
25             0.702231
26             0.226417
27             0.494766
28             0.124699
29 
30     The clock_gettime function in Linux is even faster (0.02 seconds to call
31     simple_tic 1 million times).  The random numbers are identical, as desired.
32 */
33 
34 #include "simple_rand.h"
35 #include "simple_timer.h"
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #define LEN 10000000
40 
main(void)41 int main (void)
42 {
43     double *x ;
44     double tic [2], t ;
45     int i ;
46 
47     fprintf (stderr, "simple_demo:\n") ;
48     double n = ((double) LEN) / 1e6 ;
49 
50     // calloc the space for more accurate timing
51     x = (double *) calloc (LEN, sizeof (double)) ;
52     if (x == NULL)
53     {
54         fprintf (stderr, "simple_demo: out of memory\n") ;
55         exit (1) ;
56     }
57 
58     // do lots of tics
59     simple_tic (tic) ;
60     for (i = 0 ; i < LEN/10 ; i++)
61     {
62         double tic2 [2] ;
63         simple_tic (tic2) ;
64     }
65     t = simple_toc (tic) ;
66     printf ("time to call simple_tic %g million times: %g\n", n/10, t) ;
67 
68     // generate random numbers
69     simple_tic (tic) ;
70     for (i = 0 ; i < LEN ; i++)
71     {
72         x [i] = simple_rand_x ( ) ;
73     }
74     t = simple_toc (tic) ;
75 
76     // report the result
77     printf ("time to generate %g million random numbers: %g\n", n, t) ;
78     fprintf (stderr, "time to generate %g million random numbers: %g\n\n",
79         n, t) ;
80 
81     // these should be the same on any system and any compiler
82     printf ("first 10 random numbers:\n") ;
83     for (i = 0 ; i < 10 ; i++)
84     {
85         printf ("%12.6f\n", x [i]) ;
86     }
87 
88     // generate random uint64_t numbers
89     double t1 ;
90 
91     simple_tic (tic) ;
92     for (i = 0 ; i < LEN ; i++)
93     {
94         simple_rand_i ( ) ;
95     }
96     t1 = simple_toc (tic) ;
97 
98     printf ("time to generate %g million random uint64: %g\n", n, t1) ;
99     fprintf (stderr, "time to generate %g million random uint64: %g\n\n",
100         n, t1) ;
101 
102     free (x) ;
103 }
104 
105