1 /* 2 PARTIO SOFTWARE 3 Copyright 2010 Disney Enterprises, Inc. All rights reserved 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are 7 met: 8 9 * Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 * Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the 15 distribution. 16 17 * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation 18 Studios" or the names of its contributors may NOT be used to 19 endorse or promote products derived from this software without 20 specific prior written permission from Walt Disney Pictures. 21 22 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND 23 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 25 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED. 26 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR 27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY 31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 34 */ 35 36 #ifndef _timer_h_ 37 #define _timer_h_ 38 39 #ifndef PARTIO_WIN32 40 41 #include <sys/time.h> 42 #include <iostream> 43 44 struct Timer 45 { 46 const char* name; 47 struct timeval start; 48 bool running; 49 TimerTimer50 Timer(const char* name) 51 :name(name) 52 { 53 gettimeofday(&start,0); 54 running=true; 55 } 56 Stop_TimeTimer57 double Stop_Time() 58 { 59 struct timeval end; 60 gettimeofday(&end,0); 61 double seconds=(double)1e-6*(double)((end.tv_sec * 1000000 + end.tv_usec) 62 - (start.tv_sec * 1000000 + start.tv_usec)); 63 std::cerr<<"Time for '"<<name<<"' was "<<seconds<<" s"<<std::endl; 64 running=false; 65 return seconds; 66 } 67 ~TimerTimer68 ~Timer() 69 { 70 if(running) Stop_Time(); 71 } 72 73 }; 74 75 #else 76 77 #include <windows.h> 78 #include <string> 79 80 struct Timer 81 { 82 const char* name; 83 bool running; 84 85 __int64 start; 86 __int64 counts_per_sec; 87 TimerTimer88 Timer(const char* name) 89 :name(name) 90 { 91 QueryPerformanceFrequency((LARGE_INTEGER*)&counts_per_sec); 92 QueryPerformanceCounter((LARGE_INTEGER*)&start); 93 running=true; 94 } 95 Stop_TimeTimer96 double Stop_Time() 97 { 98 __int64 end; 99 QueryPerformanceCounter((LARGE_INTEGER*)&end); 100 double seconds=float(end-start)/float(counts_per_sec); 101 std::cerr<<"Time for '"<<name<<"' was "<<seconds<<" s"<<std::endl; 102 running=false; 103 return seconds; 104 } 105 ~TimerTimer106 ~Timer() 107 { 108 if(running) Stop_Time(); 109 } 110 111 }; 112 113 114 #endif 115 116 #endif 117