-
Notifications
You must be signed in to change notification settings - Fork 10
/
speed_test.c
135 lines (105 loc) · 3.16 KB
/
speed_test.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
//: Based on code collected in the XPokerEval library at:
// http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup
//: original code from Ray Wouten, Steve Brecher, and others on twoplustwo.com
typedef int64_t LARGE_INTEGER;
typedef struct timespec sysTime_t;
#include "ace_eval.h"
#define MS_PER_SEC 1000.0f
#define LOTS 100000000 //1e6
struct timespec timings,endtimings;
const char HandRanks[][16] = {"High Card","Pair","Two Pair","Three of a Kind","Straight","Flush","Full House","Four of a Kind","BAD","Straight Flush"};
//"Knuth Shuffle" or "Fisher-Yates"
void Shuffle(Card* Deck)
{
int r,i;
Card temp;
for(i=51;i>=0;i--)
{
r=(int)(((float)rand())/RAND_MAX*i);
temp = Deck[i];
Deck[i]=Deck[r];
Deck[r]=temp;
}
}
//** Linux specific timing **/
sysTime_t platformTimeElapsed(sysTime_t now, sysTime_t then)
{
sysTime_t e;
e.tv_sec = now.tv_sec-then.tv_sec;
if (now.tv_nsec < then.tv_nsec){
e.tv_sec-=1;
e.tv_nsec = now.tv_nsec+(1e9-then.tv_nsec);
}
else{
e.tv_nsec = now.tv_nsec-then.tv_nsec;
}
return e;
}
double platformSysTimeToMs(sysTime_t timeIn) {
return (double)timeIn.tv_sec* MS_PER_SEC + (double)timeIn.tv_nsec / 1e6;
}
//Eval LOTS of random hands
Card hands[LOTS][ACEHAND]={0};
int main(int argc, char*argv[])
{
long i;
Card Deck[52];
int cardsLeft = 52;
int count = 0;
int handTypeSum[10]={0};
srand(argc);
for (i=0;i<52;i++)
{
Deck[i]=ACE_makecard(i);
}
Shuffle(Deck);
for (i=0;i<LOTS;i++)
{
if (cardsLeft<7)
{
Shuffle(Deck);
cardsLeft=52;
}
memset(hands[i],0,sizeof(Card)*ACEHAND);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
--cardsLeft;
ACE_addcard(hands[i],Deck[cardsLeft]);
}
//TIME LOTS of Evals
count = 0;
clock_t timer = clock(); // start regular clock
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &timings); //and h-p clock
for (i=0;i<LOTS;i++)
{
Card r = ACE_evaluate( hands[i] );
handTypeSum[ACE_rank(r)]++;
count++;
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &endtimings); // end the high precision clock
timer = clock() - timer; // end the regular clock
for (i = 0; i <= 9; i++) // display results
printf("\n%16s = %d", HandRanks[i], handTypeSum[i]);
printf("\nTotal Hands = %d\n", count);
double clocksused = platformSysTimeToMs(platformTimeElapsed(endtimings,timings));
// and display the clock results
printf("\nValidation seconds = %.4lf\nTotal HighPrecision Clocks = %lf\nHighPrecision clocks per lookup = %lf\n",
(double)timer/CLOCKS_PER_SEC, clocksused, (double) clocksused / count) ;
printf("\n(smaller is better...\n");
printf("\n %lf Mhands/sec\n",count/((double)clocksused/1000)/1000000.0);
}