/lib/node_modules/@stdlib/math/base/complex/inv/benchmark/c/benchmark.c

https://github.com/stdlib-js/stdlib · C · 141 lines · 74 code · 14 blank · 53 comment · 6 complexity · ed5893c59ebb37fc4ec606b3383cebf2 MD5 · raw file

  1. /**
  2. * @license Apache-2.0
  3. *
  4. * Copyright (c) 2018 The Stdlib Authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * Benchmark `cinv`.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <complex.h>
  25. #include <sys/time.h>
  26. #define NAME "inv"
  27. #define ITERATIONS 1000000
  28. #define REPEATS 3
  29. /**
  30. * Prints the TAP version.
  31. */
  32. void print_version() {
  33. printf( "TAP version 13\n" );
  34. }
  35. /**
  36. * Prints the TAP summary.
  37. *
  38. * @param total total number of tests
  39. * @param passing total number of passing tests
  40. */
  41. void print_summary( int total, int passing ) {
  42. printf( "#\n" );
  43. printf( "1..%d\n", total ); // TAP plan
  44. printf( "# total %d\n", total );
  45. printf( "# pass %d\n", passing );
  46. printf( "#\n" );
  47. printf( "# ok\n" );
  48. }
  49. /**
  50. * Prints benchmarks results.
  51. *
  52. * @param elapsed elapsed time in seconds
  53. */
  54. void print_results( double elapsed ) {
  55. double rate = (double)ITERATIONS / elapsed;
  56. printf( " ---\n" );
  57. printf( " iterations: %d\n", ITERATIONS );
  58. printf( " elapsed: %0.9f\n", elapsed );
  59. printf( " rate: %0.9f\n", rate );
  60. printf( " ...\n" );
  61. }
  62. /**
  63. * Returns a clock time.
  64. *
  65. * @return clock time
  66. */
  67. double tic() {
  68. struct timeval now;
  69. gettimeofday( &now, NULL );
  70. return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
  71. }
  72. /**
  73. * Generates a random double on the interval [0,1].
  74. *
  75. * @return random double
  76. */
  77. double rand_double() {
  78. int r = rand();
  79. return (double)r / ( (double)RAND_MAX + 1.0 );
  80. }
  81. /**
  82. * Runs a benchmark.
  83. *
  84. * @return elapsed time in seconds
  85. */
  86. double benchmark() {
  87. double elapsed;
  88. double re;
  89. double im;
  90. double t;
  91. int i;
  92. double complex z1;
  93. double complex z2;
  94. t = tic();
  95. for ( i = 0; i < ITERATIONS; i++ ) {
  96. re = ( 1000.0*rand_double() ) - 500.0;
  97. im = ( 1000.0*rand_double() ) - 500.0;
  98. z1 = re + im*I;
  99. z2 = 1.0 / z1;
  100. if ( z2 != z2 ) {
  101. printf( "should not return NaN\n" );
  102. break;
  103. }
  104. }
  105. elapsed = tic() - t;
  106. if ( z2 != z2 ) {
  107. printf( "should not return NaN\n" );
  108. }
  109. return elapsed;
  110. }
  111. /**
  112. * Main execution sequence.
  113. */
  114. int main( void ) {
  115. double elapsed;
  116. int i;
  117. // Use the current time to seed the random number generator:
  118. srand( time( NULL ) );
  119. print_version();
  120. for ( i = 0; i < REPEATS; i++ ) {
  121. printf( "# c::%s\n", NAME );
  122. elapsed = benchmark();
  123. print_results( elapsed );
  124. printf( "ok %d benchmark finished\n", i+1 );
  125. }
  126. print_summary( REPEATS, REPEATS );
  127. }