lametime.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Lame time routines source file
  3. *
  4. * Copyright (c) 2000 Mark Taylor
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /* $Id$ */
  22. /*
  23. * name: GetCPUTime ( void )
  24. *
  25. * description: returns CPU time used by the process
  26. * input: none
  27. * output: time in seconds
  28. * known bugs: may not work in SMP and RPC
  29. * conforming: ANSI C
  30. *
  31. * There is some old difficult to read code at the end of this file.
  32. * Can someone integrate this into this function (if useful)?
  33. */
  34. #ifdef HAVE_CONFIG_H
  35. # include <config.h>
  36. #endif
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #include <time.h>
  40. #ifdef WITH_DMALLOC
  41. #include <dmalloc.h>
  42. #endif
  43. #include "lametime.h"
  44. #if !defined(CLOCKS_PER_SEC)
  45. # warning Your system does not define CLOCKS_PER_SEC, guessing one...
  46. # define CLOCKS_PER_SEC 1000000
  47. #endif
  48. double
  49. GetCPUTime(void)
  50. {
  51. clock_t t;
  52. #if defined(_MSC_VER) || defined(__BORLANDC__)
  53. t = clock();
  54. #else
  55. t = clock();
  56. #endif
  57. return t / (double) CLOCKS_PER_SEC;
  58. }
  59. /*
  60. * name: GetRealTime ( void )
  61. *
  62. * description: returns real (human) time elapsed relative to a fixed time (mostly 1970-01-01 00:00:00)
  63. * input: none
  64. * output: time in seconds
  65. * known bugs: bad precision with time()
  66. */
  67. #if defined(__unix__) || defined(SVR4) || defined(BSD)
  68. # include <sys/time.h>
  69. # include <unistd.h>
  70. double
  71. GetRealTime(void)
  72. { /* conforming: SVr4, BSD 4.3 */
  73. struct timeval t;
  74. if (0 != gettimeofday(&t, NULL))
  75. assert(0);
  76. return t.tv_sec + 1.e-6 * t.tv_usec;
  77. }
  78. #elif defined(WIN16) || defined(WIN32)
  79. # include <stdio.h>
  80. # include <sys/types.h>
  81. # include <sys/timeb.h>
  82. double
  83. GetRealTime(void)
  84. { /* conforming: Win 95, Win NT */
  85. struct timeb t;
  86. ftime(&t);
  87. return t.time + 1.e-3 * t.millitm;
  88. }
  89. #else
  90. double
  91. GetRealTime(void)
  92. { /* conforming: SVr4, SVID, POSIX, X/OPEN, BSD 4.3 */ /* BUT NOT GUARANTEED BY ANSI */
  93. time_t t;
  94. t = time(NULL);
  95. return (double) t;
  96. }
  97. #endif
  98. #if defined(_WIN32) || defined(__CYGWIN__)
  99. # include <io.h>
  100. # include <fcntl.h>
  101. #else
  102. # include <unistd.h>
  103. #endif
  104. int
  105. lame_set_stream_binary_mode(FILE * const fp)
  106. {
  107. #if defined __EMX__
  108. _fsetmode(fp, "b");
  109. #elif defined __BORLANDC__
  110. setmode(_fileno(fp), O_BINARY);
  111. #elif defined __CYGWIN__
  112. setmode(fileno(fp), _O_BINARY);
  113. #elif defined _WIN32
  114. _setmode(_fileno(fp), _O_BINARY);
  115. #else
  116. (void) fp; /* doing nothing here, silencing the compiler only. */
  117. #endif
  118. return 0;
  119. }
  120. /* End of lametime.c */