QueryPerformanceCounter speed up

  • Thread starter The Last Danish Pastry
  • Start date
T

The Last Danish Pastry

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);
}
for (int i = 1; i < N; ++i)
{
Console.WriteLine(i.ToString()+": "+(a-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
 
J

Jochen Kalmbach

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...
 
J

Jochen Kalmbach

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...
 
G

Guest

There's another one called QueryPerformanceFrequency, which should tell you
the speed of it. Doesn't dividing one by the other make it arbitrary?
 
T

The Last Danish Pastry

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);
QueryPerformanceFrequency(out b);
}
for (int i = 1; i < N; ++i)
{
Console.WriteLine(i.ToString()+": "+(a-a[i-1]).ToString()+"
"+b.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)
 
T

The Last Danish Pastry

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top