1
/* This file is part of the KDE project
2
Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3
Copyright 2007 Ariya Hidayat <ariya@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#ifndef KSPREAD_BENCHMARK_HELPER
22
#define KSPREAD_BENCHMARK_HELPER
34
// FIXME: how about GCC on Mac PPC/Intel ?
36
// this depends on timing resolution
37
const long iterations = 100000;
40
typedef unsigned long tval;
43
inline tval stamp(void)
46
asm volatile("rdtsc" : "=a" (tsc) : : "edx");
50
inline tval elapsed(tval t)
53
asm volatile("rdtsc" : "=a" (tsc) : : "edx");
60
static QString printAverage( tval ticks, int counter, const QString& prefix = QString() )
64
procCpuInfo.start( "cat /proc/cpuinfo");
65
if ( procCpuInfo.waitForFinished() )
67
QRegExp reg( "cpu MHz\\s+:\\s+(\\d{4}.\\d{3})" );
68
reg.indexIn( procCpuInfo.readAllStandardOutput() );
70
double freq = reg.cap(1).toDouble( &ok );
73
double time = 1000.0 * ticks / counter / freq; // ns
75
str = QString("%1 ns/operation").arg( QString::number( time, 'f', 2 ) );
80
str = QString("%1 us/operation").arg( QString::number( time, 'f', 2 ) );
84
str = QString("%1 ms/operation").arg( QString::number( time, 'f', 2 ) );
89
return QString( "%1 Average: %2/%3=%4 cycles/operation; %5" ).
90
arg(prefix). arg( ticks ).arg( counter ).arg( ticks/counter ).arg( str );
95
// so we don't use GCC, check if it is MS VC
100
// this depends on timing resolution
101
const long iterations = 10000000;
103
// Ideally we use QueryPerformanceCounter here, but somehow I can't manage
104
// to make it work (Ariya)
108
// GetTickCount() returns elapsed time since boot in milliseconds
110
inline tval stamp(void)
112
return GetTickCount();
116
inline tval elapsed(tval t)
118
tval tsc = GetTickCount();
125
static QString printAverage( tval ticks, int counter, const QString& prefix = QString() )
128
double time = ticks * 1.0e6 / counter; // ns
130
str = QString("%1 ns/operation").arg( QString::number( time, 'f', 2 ) );
133
time /= 1000.0; // us
135
str = QString("%1 us/operation").arg( QString::number( time, 'f', 2 ) );
138
time /= 1000.0; // ms
139
str = QString("%1 ms/operation").arg( QString::number( time, 'f', 2 ) );
142
return QString( "%1 Average: %2/%3=%4 ticks/operation; %5" ).
143
arg(prefix). arg( ticks ).arg( counter ).arg( ticks/counter ).arg( str );
148
#error Unsupported compiler and platform !
156
} // namespace KSpread
158
#endif // KSPREAD_BENCHMARK_HELPER