473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

QueryPerformanceCounter speed up

If I run this program:
=======================================
using System;
using System.Runtime.InteropServices;

namespace QPC
{
class Class1
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);

[STAThread]
static void Main(string[] args)
{
const int N = 120;
long[] a = new long[N];
for (int i = 0; i < N; ++i)
{
QueryPerformanceCounter(out a[i]);
}
for (int i = 1; i < N; ++i)
{
Console.WriteLine(i.ToString()+": "+(a[i]-a[i-1]).ToString());
}
Console.ReadLine();
} // Main

} // Class1

} // QPC
=======================================
I get this output:
=======================================
1: 37
2: 26
3: 26
4: 25
5: 25
6: 26
7: 25
8: 25
9: 25
10: 26
11: 448
12: 38
13: 25
14: 25
15: 24
16: 25
17: 24
18: 25
19: 25
20: 25
21: 25
22: 27
23: 25
24: 25
25: 25
26: 26
27: 25
28: 25
29: 25
30: 26
31: 25
32: 25
33: 25
34: 26
35: 25
36: 25
37: 25
38: 26
39: 25
40: 25
41: 25
42: 25
43: 24
44: 25
45: 24
46: 26
47: 25
48: 25
49: 25
50: 26
51: 25
52: 25
53: 25
54: 25
55: 25
56: 25
57: 25
58: 25
59: 25
60: 25
61: 24
62: 26
63: 24
64: 25
65: 24
66: 25
67: 25
68: 25
69: 24
70: 26
71: 25
72: 25
73: 25
74: 25
75: 25
76: 26
77: 25
78: 26
79: 24
80: 26
81: 26
82: 26
83: 25
84: 26
85: 25
86: 26
87: 25
88: 25
89: 25
90: 25
91: 24
92: 25
93: 24
94: 25
95: 25
96: 25
97: 24
98: 26
99: 24
100: 1564
101: 10
102: 6
103: 6
104: 5
105: 6
106: 7
107: 6
108: 6
109: 5
110: 6
111: 5
112: 6
113: 5
114: 7
115: 6
116: 5
117: 6
118: 6
119: 6
=======================================

Why, after 100 calls to QPC, does it suddenly speed up?

This behavior is consistent. I have run it on three computers running
different flavours of Windows.

I am using:
Microsoft Development Environment 2003 Version 7.1.3088
Microsoft .NET Framework 1.1 Version 1.1.4322

--
Clive Tooth
http://www.clivetooth.dk
Nov 16 '05 #1
5 9884
Hy "The Last Danish Pastry",

just to inform you: I have exactly the same behaviour!!!

It would be nice if MS could help to bring up light into this "misterious"
times...

---
Greetings
Jochen
Nov 16 '05 #2
Hello "The Last Danish Pastry",

it seems to have something to do with Code-Security and unmanaged code!

If you specify the "SuppressUnmanagedCodeSecurity" attribute on the
dllimport method you will be constantly faster:

[DllImport("Kernel32.dll")]
[System.Security.SuppressUnmanagedCodeSecurity()]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);

It seems that MS implemented some kind of "speed up" if an unmanaged
function was called more than 99 times...
If this is the case, and the CLI will NOT make an Demand on this unmanaged
function, then this will be a security hole...

---
Greetings
Jochen
Nov 16 '05 #3
There's another one called QueryPerformanceFrequency, which should tell you
the speed of it. Doesn't dividing one by the other make it arbitrary?
"The Last Danish Pastry" wrote:
If I run this program:
=======================================
using System;
using System.Runtime.InteropServices;

namespace QPC
{
class Class1
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);

[STAThread]
static void Main(string[] args)
{
const int N = 120;
long[] a = new long[N];
for (int i = 0; i < N; ++i)
{
QueryPerformanceCounter(out a[i]);
}
for (int i = 1; i < N; ++i)
{
Console.WriteLine(i.ToString()+": "+(a[i]-a[i-1]).ToString());
}
Console.ReadLine();
} // Main

} // Class1

} // QPC
=======================================
I get this output:
=======================================
1: 37
2: 26
3: 26
4: 25
5: 25
6: 26
7: 25
8: 25
9: 25
10: 26
11: 448
12: 38
13: 25
14: 25
15: 24
16: 25
17: 24
18: 25
19: 25
20: 25
21: 25
22: 27
23: 25
24: 25
25: 25
26: 26
27: 25
28: 25
29: 25
30: 26
31: 25
32: 25
33: 25
34: 26
35: 25
36: 25
37: 25
38: 26
39: 25
40: 25
41: 25
42: 25
43: 24
44: 25
45: 24
46: 26
47: 25
48: 25
49: 25
50: 26
51: 25
52: 25
53: 25
54: 25
55: 25
56: 25
57: 25
58: 25
59: 25
60: 25
61: 24
62: 26
63: 24
64: 25
65: 24
66: 25
67: 25
68: 25
69: 24
70: 26
71: 25
72: 25
73: 25
74: 25
75: 25
76: 26
77: 25
78: 26
79: 24
80: 26
81: 26
82: 26
83: 25
84: 26
85: 25
86: 26
87: 25
88: 25
89: 25
90: 25
91: 24
92: 25
93: 24
94: 25
95: 25
96: 25
97: 24
98: 26
99: 24
100: 1564
101: 10
102: 6
103: 6
104: 5
105: 6
106: 7
107: 6
108: 6
109: 5
110: 6
111: 5
112: 6
113: 5
114: 7
115: 6
116: 5
117: 6
118: 6
119: 6
=======================================

Why, after 100 calls to QPC, does it suddenly speed up?

This behavior is consistent. I have run it on three computers running
different flavours of Windows.

I am using:
Microsoft Development Environment 2003 Version 7.1.3088
Microsoft .NET Framework 1.1 Version 1.1.4322

--
Clive Tooth
http://www.clivetooth.dk

Nov 16 '05 #4
"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:72**********************************@microsof t.com...
There's another one called QueryPerformanceFrequency, which should tell you the speed of it. Doesn't dividing one by the other make it arbitrary?


The result returned by QueryPerformanceFrequency never changes.
I modified the program a little:
#####################################
using System;
using System.Runtime.InteropServices;

namespace QPC
{
class Class1
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long
lpFrequency);

[STAThread]
static void Main(string[] args)
{
const int N = 120;
long[] a = new long[N];
long[] b = new long[N];
for (int i = 0; i < N; ++i)
{
QueryPerformanceCounter(out a[i]);
QueryPerformanceFrequency(out b[i]);
}
for (int i = 1; i < N; ++i)
{
Console.WriteLine(i.ToString()+": "+(a[i]-a[i-1]).ToString()+"
"+b[i].ToString());
}
Console.ReadLine();
} // Main

} // Class1

} // QPC
#####################################
.... and this time I got...
#####################################
1: 536 3579545
2: 50 3579545
3: 50 3579545
4: 50 3579545
5: 50 3579545
6: 196 3579545
7: 50 3579545
8: 49 3579545
9: 51 3579545
10: 50 3579545
11: 52 3579545
12: 50 3579545
13: 52 3579545
14: 51 3579545
15: 50 3579545
16: 50 3579545
17: 50 3579545
18: 50 3579545
19: 50 3579545
20: 50 3579545
21: 50 3579545
22: 50 3579545
23: 50 3579545
24: 50 3579545
25: 51 3579545
26: 50 3579545
27: 50 3579545
28: 50 3579545
29: 50 3579545
30: 50 3579545
31: 50 3579545
32: 50 3579545
33: 50 3579545
34: 50 3579545
35: 51 3579545
36: 50 3579545
37: 50 3579545
38: 50 3579545
39: 51 3579545
40: 51 3579545
41: 50 3579545
42: 50 3579545
43: 50 3579545
44: 50 3579545
45: 50 3579545
46: 50 3579545
47: 50 3579545
48: 50 3579545
49: 50 3579545
50: 1319 3579545
51: 20 3579545
52: 12 3579545
53: 11 3579545
54: 11 3579545
55: 11 3579545
56: 11 3579545
57: 11 3579545
58: 11 3579545
59: 12 3579545
60: 11 3579545
61: 11 3579545
62: 12 3579545
63: 11 3579545
64: 11 3579545
65: 11 3579545
66: 12 3579545
67: 11 3579545
68: 11 3579545
69: 11 3579545
70: 11 3579545
71: 12 3579545
72: 11 3579545
73: 12 3579545
74: 12 3579545
75: 11 3579545
76: 11 3579545
77: 11 3579545
78: 12 3579545
79: 13 3579545
80: 12 3579545
81: 12 3579545
82: 11 3579545
83: 12 3579545
84: 13 3579545
85: 12 3579545
86: 11 3579545
87: 11 3579545
88: 11 3579545
89: 11 3579545
90: 12 3579545
91: 11 3579545
92: 11 3579545
93: 11 3579545
94: 11 3579545
95: 11 3579545
96: 11 3579545
97: 12 3579545
98: 11 3579545
99: 11 3579545
100: 11 3579545
101: 11 3579545
102: 11 3579545
103: 11 3579545
104: 12 3579545
105: 11 3579545
106: 11 3579545
107: 11 3579545
108: 13 3579545
109: 11 3579545
110: 11 3579545
111: 12 3579545
112: 12 3579545
113: 12 3579545
114: 11 3579545
115: 11 3579545
116: 11 3579545
117: 11 3579545
118: 11 3579545
119: 11 3579545
#####################################
So it speeds up after a TOTAL of 100 calls! (50 to QPC and 50 to QPF)

--
Clive Tooth
http://www.clivetooth.dk
Nov 16 '05 #5
"Jochen Kalmbach" <no********************@holzma.de> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hello "The Last Danish Pastry",

it seems to have something to do with Code-Security and unmanaged code!

If you specify the "SuppressUnmanagedCodeSecurity" attribute on the
dllimport method you will be constantly faster:

[DllImport("Kernel32.dll")]
[System.Security.SuppressUnmanagedCodeSecurity()]
private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);

It seems that MS implemented some kind of "speed up" if an unmanaged
function was called more than 99 times...
If this is the case, and the CLI will NOT make an Demand on this unmanaged
function, then this will be a security hole...


Well, well !
Although how there could be any security implications in calling
QueryPerformanceCounter is not clear to me.

--
Clive Tooth
http://www.clivetooth.dk
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Yang Li Ke | last post by:
Hi guys, Is it possible to know the internet speed of the visitors with php? Thanx -- Yang
8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
34
by: Jacek Generowicz | last post by:
I have a program in which I make very good use of a memoizer: def memoize(callable): cache = {} def proxy(*args): try: return cache except KeyError: return cache.setdefault(args,...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
6
by: Ham | last post by:
Yeah, Gotto work with my VB.Net graphic application for days, do any possible type of code optimization, check for unhandled errors and finally come up with sth that can't process 2D graphics and...
0
by: vermarajeev | last post by:
Can anyone please help me with this. I want the linux equivalent of this code. int entropy_fun(unsigned char buf, unsigned int len) { unsigned __int64 pentium_tsc; unsigned...
6
by: Jassim Rahma | last post by:
I want to detect the internet speed using C# to show the user on what speed he's connecting to internet?
11
by: kyosohma | last post by:
Hi, We use a script here at work that runs whenever someone logs into their machine that logs various bits of information to a database. One of those bits is the CPU's model and speed. While...
4
by: nestle | last post by:
I have DSL with a download speed of 32MB/s and an upload speed of 8MB/s(according to my ISP), and I am using a router. My upload speed is always between 8MB/s and 9MB/s(which is above the max upload...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.