-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpuspeed.cc
More file actions
109 lines (88 loc) · 2.91 KB
/
cpuspeed.cc
File metadata and controls
109 lines (88 loc) · 2.91 KB
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
/*
Copyright (C) 2008 Maks Verver
Copyright (C) 2007 Thijs Marinussen
Copyright (C) 2005 Jaap Taal and Marcel Vlastuin.
This file is part of the Caia project. This project can be
downloaded from the website www.codecup.nl. You can email to
marcel@vlastuin.net or write to Marcel Vlastuin, Perenstraat 40,
2564 SE Den Haag, The Netherlands.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/*
getTSC saves the current value of the time stamp counter in *counter
*/
#if __i386
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
inline void getTSC(unsigned long long *counter)
{
__asm__("rdtsc\n"
"movl %%eax, (%%edi)\n"
"movl %%edx, 4(%%edi)\n"
: : "D" (counter) : "eax","edx");
}
int cpuspeed(void)
{
unsigned long long start,stop,clocks;
timeval ts;
ts.tv_sec = 0;
ts.tv_usec = 100000; //Sleep 100.000 usec = 0.1 sec
getTSC(&start);
select(0,NULL,NULL,NULL,&ts);
getTSC(&stop);
clocks = stop-start; //Clocks now contains the number of clockticks in 0.1 second
//The number of clockticks divided by 100.000 is the speed of the processor in Mhz
//fprintf(stderr,"clocks=%llu\n",clocks);fflush(stderr);
return clocks / 100000;
}
#elif __x86_64
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
inline void getTSC(unsigned long long *counter)
{
__asm__("rdtsc\n"
"movl %%eax, (%%rdi)\n"
"movl %%edx, 4(%%rdi)\n"
: : "D" (counter) : "rax","rdx");
}
int cpuspeed(void)
{
unsigned long long start,stop,clocks;
timeval ts;
ts.tv_sec = 0;
ts.tv_usec = 100000; //Sleep 100.000 usec = 0.1 sec
getTSC(&start);
select(0,NULL,NULL,NULL,&ts);
getTSC(&stop);
clocks = stop-start; //Clocks now contains the number of clockticks in 0.1 second
//The number of clockticks divided by 100.000 is the speed of the processor in Mhz
//fprintf(stderr,"clocks=%llu\n",clocks);fflush(stderr);
return clocks / 100000;
}
#else
#include <stdio.h>
#include <sys/time.h>
int cpuspeed(void)
{
long dif, count;
volatile double dummy=1.0;
struct timeval start, stop;
gettimeofday(&start, NULL);
for (count = 0; count < 50000000; ++count) dummy/=2;
gettimeofday(&stop, NULL);
dif = 1000000 * (stop.tv_sec - start.tv_sec) + stop.tv_usec - start.tv_usec;
return int(266.0*5210000.0/float(dif))+(int)dummy;
}
#endif