0

I'm making a blocking algorithm, and I just realised that adding a timeout to such algorithm is not so easy if it should remain precise.
Adding timeout means, that the blocking algorithm should abort after X ms if not earlier. Now I seem to have two options:

  1. Iterating time (has mistake, but is fast)
    • Check blocking condition
    • Iterate time_elapsed by 1 (which means 1e-6 sec with use of usleep)
    • Compare time_elapsed with timeout. (here is the problem I will talk about)
    • usleep(1)
  2. Getting system time every iteration (slow, but precise)
    • I know how to do this, please do not post any answers about that.

Compating timeout with time_elapsed

And here is what bothers me. The timeout will be in milliseconds (10e-3) while usleep sleeps for 10e-6 seconds. So my time_elapsed will be 1000 times more precise than timeout. I want to truncate last three digits of time_elapsed (operation equal to floor($time_elapsed/1000) without dividing it. Division algorithm is too slow.

Summary

I want to make my variable 1000 times smaller without dividing it by 1000. I want just get rid of the data. In binary I'd use bit-shift operator, but have no idea how to apply it on decimal system.

Code sample:

Sometimes, when people on SO cannot answer the theoretical question, they really hunger for the code. Here it is:

floor($time_elapsed/1000);

I want to replace this code with something much faster. Please note that though the question itself is full of timeouts, the question title is only about truncating that data. Other users may find the solution useful for other purposes than timing.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Can se see your code so far? – showdev Apr 17 '13 at 18:34
  • Don't you understand the explanation? My code strictly follows steps in point `1.` of my solution list. Only thing that matters is the `floor($time_elapsed/1000)` which I need to be replaced by a faster calculation. – Tomáš Zato Apr 17 '13 at 18:37
  • No, I don't fully understand your explanation. Posting the code you have tried would help. Why not just do everything in milliseconds? – showdev Apr 17 '13 at 18:41
  • Added the code with a bit of irony. Please stick to the question title. The solution may serve to various purposes. – Tomáš Zato Apr 17 '13 at 18:50
  • No need to be snarky. Your explanation may not be as clear as you think it is. Look it up yourself: http://stackoverflow.com/questions/5284898/implement-division-with-bit-wise-operator – showdev Apr 17 '13 at 18:55
  • Incidentally, I wanted the code you tried for the bit-shifting. Not the useless `floor()` code that you don't want to use. – showdev Apr 17 '13 at 19:06
  • the division takes 0,000006 microseconds..you really need to optimize that? – bitWorking Apr 17 '13 at 19:08
  • @showdev: I do not have it. My question is about how should such code look like (and why). – Tomáš Zato Apr 17 '13 at 19:12
  • @redreggae The checking algorithm altogether turned 10 sec timeout to 10.9. Do not forget that there are **thousands** of iterations. That turns 0.000006 to 0.6 in a swift. – Tomáš Zato Apr 17 '13 at 19:12

2 Answers2

0

Maybe this will help Php number format. Though this does cause rounding, if that is unacceptable then I don't think its possible because PHP is loosely typed that you cant define numbers with a particular level of precision.

Useless Intern
  • 1,294
  • 1
  • 10
  • 20
  • Thank you for your effort, but this solution is even slower. When you compare string operations to number operations you are comparing bullet to hour hand on clock. – Tomáš Zato Apr 17 '13 at 18:54
  • Then the absolute last thing I can think of is rounding, after this I got nothing. – Useless Intern Apr 17 '13 at 19:00
0

try this:

(int)($time_elapsed*0.001)

this should be a lot faster

bitWorking
  • 12,485
  • 1
  • 32
  • 38