~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kspread/tests/BenchmarkHelper.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
4
 
 
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.
9
 
 
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.
14
 
 
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.
19
 
*/
20
 
 
21
 
#ifndef KSPREAD_BENCHMARK_HELPER
22
 
#define KSPREAD_BENCHMARK_HELPER
23
 
 
24
 
#include <QProcess>
25
 
#include <QRegExp>
26
 
 
27
 
namespace KSpread
28
 
{
29
 
namespace Time
30
 
{
31
 
 
32
 
#ifdef Q_CC_GNU
33
 
 
34
 
// FIXME: how about GCC on Mac PPC/Intel ?
35
 
 
36
 
// this depends on timing resolution
37
 
const long iterations = 100000;
38
 
 
39
 
 
40
 
typedef unsigned long tval;
41
 
 
42
 
 
43
 
inline tval stamp(void) 
44
 
{
45
 
        tval tsc;
46
 
        asm volatile("rdtsc" : "=a" (tsc) : : "edx");
47
 
        return tsc;
48
 
}
49
 
 
50
 
inline tval elapsed(tval t)
51
 
{
52
 
        tval tsc;
53
 
        asm volatile("rdtsc" : "=a" (tsc) : : "edx");
54
 
        if (tsc>t)
55
 
                return tsc-t;
56
 
        else
57
 
                return t-tsc;
58
 
}
59
 
 
60
 
static QString printAverage( tval ticks, int counter, const QString& prefix = QString() )
61
 
{
62
 
    QString str;
63
 
    QProcess procCpuInfo;
64
 
    procCpuInfo.start( "cat /proc/cpuinfo");
65
 
    if ( procCpuInfo.waitForFinished() )
66
 
    {
67
 
        QRegExp reg( "cpu MHz\\s+:\\s+(\\d{4}.\\d{3})" );
68
 
        reg.indexIn( procCpuInfo.readAllStandardOutput() );
69
 
        bool ok = true;
70
 
        double freq = reg.cap(1).toDouble( &ok );
71
 
        if ( ok )
72
 
        {
73
 
            double time = 1000.0 * ticks / counter / freq; // ns
74
 
            if ( time < 1000.0 )
75
 
                str = QString("%1 ns/operation").arg( QString::number( time, 'f', 2 ) );
76
 
            else
77
 
            {
78
 
                time /= 1000.0; // us
79
 
                if ( time < 1000.0 )
80
 
                    str = QString("%1 us/operation").arg( QString::number( time, 'f', 2 ) );
81
 
                else
82
 
                {
83
 
                    time /= 1000.0; // ms
84
 
                    str = QString("%1 ms/operation").arg( QString::number( time, 'f', 2 ) );
85
 
                }
86
 
            }
87
 
        }
88
 
    }
89
 
    return QString( "%1 Average: %2/%3=%4 cycles/operation; %5" ).
90
 
      arg(prefix). arg( ticks ).arg( counter ).arg( ticks/counter ).arg( str );
91
 
}
92
 
 
93
 
#else
94
 
 
95
 
// so we don't use GCC, check if it is MS VC
96
 
#ifdef Q_CC_MSVC
97
 
 
98
 
#include <windows.h>
99
 
 
100
 
// this depends on timing resolution
101
 
const long iterations = 10000000;
102
 
 
103
 
// Ideally we use QueryPerformanceCounter here, but somehow I can't manage 
104
 
// to make it work (Ariya)
105
 
 
106
 
typedef int tval;
107
 
 
108
 
// GetTickCount() returns elapsed time since boot in milliseconds
109
 
 
110
 
inline tval stamp(void) 
111
 
{
112
 
  return GetTickCount();
113
 
}
114
 
 
115
 
 
116
 
inline tval elapsed(tval t)
117
 
{
118
 
  tval tsc = GetTickCount();
119
 
  if (tsc>t)
120
 
    return tsc-t;
121
 
  else
122
 
    return t-tsc;
123
 
}
124
 
 
125
 
static QString printAverage( tval ticks, int counter, const QString& prefix = QString() )
126
 
{
127
 
  QString str;
128
 
  double time = ticks * 1.0e6 / counter; // ns
129
 
  if ( time < 1000.0 )
130
 
    str = QString("%1 ns/operation").arg( QString::number( time, 'f', 2 ) );
131
 
  else
132
 
  {
133
 
    time /= 1000.0; // us
134
 
    if ( time < 1000.0 )
135
 
      str = QString("%1 us/operation").arg( QString::number( time, 'f', 2 ) );
136
 
    else
137
 
    {
138
 
      time /= 1000.0; // ms
139
 
      str = QString("%1 ms/operation").arg( QString::number( time, 'f', 2 ) );
140
 
    }
141
 
  }
142
 
  return QString( "%1 Average: %2/%3=%4 ticks/operation; %5" ).
143
 
  arg(prefix). arg( ticks ).arg( counter ).arg( ticks/counter ).arg( str );
144
 
}
145
 
 
146
 
#else
147
 
 
148
 
#error Unsupported compiler and platform !
149
 
 
150
 
#endif
151
 
 
152
 
#endif
153
 
 
154
 
 
155
 
} // namespace Time
156
 
} // namespace KSpread
157
 
 
158
 
#endif // KSPREAD_BENCHMARK_HELPER