00001 //---------------------------------------------------------------------------- 00002 /** @file GoBoardCheckPerformance.cpp 00003 See GoBoardCheckPerformance.h 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #include "SgSystem.h" 00008 #include "GoBoardCheckPerformance.h" 00009 00010 #include <fstream> 00011 #include "GoBoard.h" 00012 #include "GoBoardUtil.h" 00013 #include "SgTime.h" 00014 00015 using namespace std; 00016 00017 //---------------------------------------------------------------------------- 00018 00019 void GoBoardCheckPerformance::CheckPerformance(const GoBoard& board, 00020 ostream& out) 00021 { 00022 const int NUM_REPETITIONS = 10000; 00023 int i; 00024 00025 double startTime = SgTime::Get(); 00026 int sum1 = 0; 00027 for (i = 0; i < NUM_REPETITIONS; ++i) 00028 { 00029 for (SgPoint p = 0; p < SG_MAXPOINT; ++p) 00030 { if (board.IsEmpty(p)) 00031 sum1 += p; 00032 } 00033 } 00034 double endTime1 = SgTime::Get(); 00035 00036 int sum2 = 0; 00037 for (i = 0; i < NUM_REPETITIONS; ++i) 00038 { 00039 for (SgPoint p = board.FirstBoardPoint(); p <= board.LastBoardPoint(); 00040 ++p) 00041 { if (board.IsEmpty(p)) 00042 sum2 += p; 00043 } 00044 } 00045 double endTime2 = SgTime::Get(); 00046 00047 int sum3 = 0; 00048 for (i = 0; i < NUM_REPETITIONS; ++i) 00049 for (GoBoard::Iterator it(board); it; ++it) 00050 { 00051 if (board.IsEmpty(*it)) 00052 sum3 += *it; 00053 } 00054 double endTime3 = SgTime::Get(); 00055 00056 int sum4 = 0; 00057 for (i = 0; i < NUM_REPETITIONS; ++i) 00058 { 00059 for (SgPoint p = 0; p < SG_MAXPOINT; ++p) 00060 { 00061 if (board.IsEmpty(p)) 00062 sum4 += p; 00063 } 00064 } 00065 double endTime4 = SgTime::Get(); 00066 00067 int sum5 = 0; 00068 for (i = 0; i < NUM_REPETITIONS; ++i) 00069 { 00070 for (SgPoint p = board.FirstBoardPoint(); p <= board.LastBoardPoint(); 00071 ++p) 00072 { 00073 if (board.IsEmpty(p)) 00074 sum5 += p; 00075 } 00076 } 00077 double endTime5 = SgTime::Get(); 00078 00079 SG_ASSERT(sum1 == sum2); 00080 SG_ASSERT(sum2 == sum3); 00081 SG_ASSERT(sum3 == sum4); 00082 SG_ASSERT(sum4 == sum5); 00083 00084 double time1 = endTime1 - startTime; 00085 double time2 = endTime2 - endTime1; 00086 double time3 = endTime3 - endTime2; 00087 double time4 = endTime4 - endTime3; 00088 double time5 = endTime5 - endTime4; 00089 00090 out << "Time1: " << time1 << " For 0..SG_MAXPOINT\n" 00091 << "Time2: " << time2 << " First/LastBoardPoint\n" 00092 << "Time3: " << time3 << " GoBoard::Iterator\n" 00093 << "Time4: " << time4 << " For 0..SG_MAXPOINT, no dependency\n" 00094 << "Time5: " << time5 << " First/LastBoardPoint, no dependency\n"; 00095 }