File: speed_program.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (69 lines) | stat: -rw-r--r-- 1,574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin speed_program.cpp}

Example Use of SpeedTest
########################

Program
*******
{xrst_spell_off}
{xrst_code cpp} */
# include <cppad/utility/speed_test.hpp>

// Some compilers have gotten smarter and do not calculate something that is not used
// so make the result a global.
double global_result = 0.0;

std::string Test(size_t size, size_t repeat)
{  // setup
   double *a = new double[size];
   double *b = new double[size];
   size_t i  = size;;
   while(i)
   {  --i;
      a[i] = double(i);
      b[i] = double(2 * i);
   }
   // operations we are timing
   while(repeat--)
   {  i = size;;
      while(i)
      {  --i;
         global_result += a[i] * b[i];
      }
   }
   // teardown
   delete [] a;
   delete [] b;

   // return a test name that is valid for all sizes and repeats
   return "double: result = sum_i a[i] * b[i]";
}
int main(void)
{
   CppAD::SpeedTest(Test, 20, 20, 100);
   return 0;
}

/* {xrst_code}
{xrst_spell_on}

Output
******
Executing of the program above generated the following output
(the rates will be different for each particular system):
::

   double: c[*] = a[*] + b[*]
   size = 20  rate = 7,157,515
   size = 40  rate = 3,887,214
   size = 60  rate = 2,685,214
   size = 80  rate = 2,032,124
   size = 100 rate = 1,657,828

{xrst_end speed_program.cpp}
*/