How to measure time with microsecond precision on a 32-bit system?

In PHP 5.4.x bug #64370 was fixed at 5.4.14 so your PHP version must be still too old for this problem to manifest and you get pretty meaningful measurements.

So these results are roughly consistent with mine - you seem to be getting some minor accuracy variations that are hard to explain (perhaps it’s because floats on 64-bit system are more accurate?) but generally the accuracy is around 1/64s.

Thanks guys for testing, the moral of the story is that 64-bit Windows doesn’t help and it’s some internal PHP problem that is hard to resolve by the devs. Well, it looks like we have to live with it.

Okay, so I upgraded to PHP 5.5.7-1~dotdeb.1

Through Apache 2.2.22

First Post Duration: 0.0046329498291016
First Post Duration: 0.0046679973602295
First Post Duration: 0.0050249099731445
First Post Duration: 0.0050790309906006
First Post Duration: 0.0042960643768311
First Post Duration: 0.0034949779510498
First Post Duration: 0.0015449523925781
First Post Duration: 0.0015799999237061
First Post Duration: 0.0015418529510498
First Post Duration: 0.001471996307373
Second Post Duration: 0.001558
Second Post Duration: 0.001532
Second Post Duration: 0.001640
Second Post Duration: 0.001737
Second Post Duration: 0.001674
Second Post Duration: 0.001706
Second Post Duration: 0.001642
Second Post Duration: 0.001712
Second Post Duration: 0.001661
Second Post Duration: 0.001602

Through PHP directly:

First Post Duration: 0.0017070770263672
First Post Duration: 0.0016820430755615
First Post Duration: 0.0013980865478516
First Post Duration: 0.0014219284057617
First Post Duration: 0.0014159679412842
First Post Duration: 0.0013940334320068
First Post Duration: 0.0014259815216064
First Post Duration: 0.0015621185302734
First Post Duration: 0.0015759468078613
First Post Duration: 0.0015280246734619
Second Post Duration: 0.001577
Second Post Duration: 0.001821
Second Post Duration: 0.001600
Second Post Duration: 0.001687
Second Post Duration: 0.001730
Second Post Duration: 0.001730
Second Post Duration: 0.001506
Second Post Duration: 0.001730
Second Post Duration: 0.001721
Second Post Duration: 0.001707

So it seems they fixed it somewhere? Or 64 bit isn’t affected…

If your php version is dotdeb isn’t it running on Debian… ?

Yes, but the package I pulled (php 5.5.7) was from dotdeb.org, as they give you access to the latest PHP/Apache packages without running the entire system in the Testing/Unstable repo officially offered by Debian. Debian’s stable repo is only permitting 5.4.4, so my choices were, compile from source, or find a repo that gave me a more “latest” version of PHP than Debian’s stable (I choose the latter for convenience sake).

I was asking because this problem seems to exist only on Windows systems, so if you are running linux then I suppose any php version will work well for microtime :slight_smile:

Ah, somehow missed that. I’ll have a few tests shortly from a Windows 64 bit machine soon.

Windows 64 bit PHP 5.4.7 (I know, I’ll upgrade it here shortly ;)), Apache 2.4.3


First Post Duration: 0.0030810832977295
First Post Duration: 0.002979040145874
First Post Duration: 0.0030350685119629
First Post Duration: 0.0029799938201904
First Post Duration: 0.0029809474945068
First Post Duration: 0.0030879974365234
First Post Duration: 0.0029699802398682
First Post Duration: 0.0030460357666016
First Post Duration: 0.0030150413513184
First Post Duration: 0.0029900074005127
Second Post Duration: 0.003035
Second Post Duration: 0.002988
Second Post Duration: 0.002987
Second Post Duration: 0.003074
Second Post Duration: 0.002072
Second Post Duration: 0.001888
Second Post Duration: 0.001881
Second Post Duration: 0.001914
Second Post Duration: 0.001915
Second Post Duration: 0.001895

Windows 5.4.22, 32 bit process (it seems), Apache 2.4.7

First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.002000093460083
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
Second Post Duration: 0.000000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.000000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000

PHP 5.4.22 directly on a 64 bit machine:

First Post Duration: 0
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0
First Post Duration: 0
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010008811950684
Second Post Duration: 0.001000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.000000
Second Post Duration: 0.002000
Second Post Duration: 0.001000

Thanks. It seems the problem happens on all newer php versions on windows regardless of “bitness”.

Yeah, I’m going to run it against 5.5 once I get that setup too (just so you can see those results too).

Windows (32 bit process through apache) PHP 5.5.6, Apache 2.4.7

First Post Duration: 0.0010001659393311
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.002000093460083
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0.00099992752075195
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.000000
Second Post Duration: 0.002000
Second Post Duration: 0.001000
Second Post Duration: 0.000000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.000000

PHP directly (should be a 64 bit process)

First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0
First Post Duration: 0
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0.0010001659393311
First Post Duration: 0.00099992752075195
First Post Duration: 0.00099992752075195
First Post Duration: 0
Second Post Duration: 0.000000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000
Second Post Duration: 0.001000

Thanks, it’s all clear now. I wonder if it’s possible to grab the system time in windows directly through an exec() call. I’ve just read a few discussions about this and it turns out Windows natively supports 1/64s time resolution and the same problem exists in other languages. To be able to get a finer accuracy one needs to use an additional timer library. From what I understand PHP used to have code that emulated the more accurate timer on Windows but the problem was it sometimes got very inaccurate so they had to stop using it. It looks like there’s little hope, then.

yikes! Guess that’s another reason to run PHP on Linux :slight_smile: j/k, don’t want to start that flame war.

What if you wrote a small .NET application to get the ticks? I know .NET handles the task fine, and you could probably make it a service that is callable from PHP. However, you’ll have the communication to the application within your duration time (that is the downside).

Why a flame war? :slight_smile: I think most people will agree that Linux is better for running PHP and the like :). I just prefer Windows as my development OS and additionally I have the benefit that my code is tested on both platforms.

Unfortunately, I don’t know .NET. Anyway, this might be too much hassle - if I have the need I may simply upload the script to a linux server and test it there :).

A desktop version of .NET would simply consist of the following:

You’d have a UI field to input/select your PHP file you want executed and a label to write the milliseconds to. Then your code would be close to (I may have made a few typos since I’m at home right now)
[highlight=.NET]var phpFile = txtPHPFile.Text;
var timer = new System.Diagnostics.StopWatch();
var phpScriptProcess = new System.Diagnostics.Process();
phpScriptProcess.StartInfo.FileName = “<path to php executable>”;
phpScriptProcess.StartInfo.Arguments = phpFile;
phpScriptProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; // Set to Hidden or None if you don’t care to see the output it produces

// Start the Timer
timer.Start();

// Execute your PHP script
phpScriptProcess.Start();
phpScriptProcess.WaitForExit();// Waits here for the process to exit.

// End the Timer
timer.Stop();

// Output the time in milliseconds
lblTimeTaken.Text = timer.ElapsedMilliseconds;



Very small, very easy :) Of course, that one executes it for you and requires you to manually key that data in. You can also make a service that accepts a PHP Script File Path, and returns the milliseconds taken (you just create a Web Service project instead of a Windows Form project -- same code applies; adjust how you read the php file and change the lblTimeTaken to a return statement).

I know I've used this technique (not with PHP execution, but with other timing performances within .NET and their related processes) and it worked just fine for me.

Thanks, cpradio, I may try it once I have more spare time :slight_smile: