News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

high-precision (callback) timer (1ms resolution)

Started by M4D45M, August 15, 2006, 07:44:55 PM

Previous topic - Next topic

M4D45M

hi to all the cpp-gurus out there, (to the non-cpp-gurus aswell  :bdg  (i'm also a non-cpp-g.))

as all the (NT-based)-windows-functions do not provide 1ms resolution for time-functions,
(CreateWaitableTimer, SetTimer, Sleep, ..)
i wanted to find out how to get around this but there seems to be no simple way.
good read:
  http://www.geisswerks.com/ryan/FAQS/timing.html

it suggests to use 'timerBeginPeriod, 1/'timerEndPeriod, 1'' pair to setup time-funktions being as precise
as the supplied value in milliseconds. but it simply doesn't work.
'SetTimer' seems to not be affected by this by any means and 'fn Sleep, 1' still varies in a range from 1 to 20 ms
which is totally inacceptable!  :tdown

i know that this is due to the 10ms time-slice by the scheduler.

one way (proposed in the wild) was to use 'QueryPerformanceCounter' in an endless polling loop within a seperate thread,
while using 'fn Sleep, 0' to be more polite to the system.
but i hate the idea to render 100% cpu. this way just suxx.
this is a crappy dirty way to do it and imagine to use this on notebooks, your battery will be down in rush!  :tdown


then i found some very promising code,  :U but unfortunately it's cpp, and i'm unable to port it to asm
or at least make it available for using within an asm program, coz of lack of knowledge in cpp.  :dazzled:

plz have a look at it, it's rather short and should be very easy to reproduce by someone familiar with cpp.
(any compilation which would make it available for using in asm programs should do the job either.)

they noted that it worx while the cpu just idles around  0%  :U
and that the error level of this timer is below 5%  :U
  http://www.codeguru.com/Cpp/W-P/system/timers/article.php/c5759


would be great to have such a quality routine working from asm!
(as unprecise callback-intervals are an common issue in win32)





hutch--

Somebody has been feeding you nonsense again. Use a multimedia timer. To see how its done look at the masm32 example sleepex.

If you need higher resolution, you can try your luck with QueryPerformanceCounter() but a ring0 driver is the right way to do it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

M4D45M

#2
ok, i'll try using a multimedia timer. (but i don't think this will give 1ms precision)
and who the f**k was feeding me nonsense, please? and even again? didn't knew about that. so thanks very much. :(

as i'm not into ring0, i think i'll stick to a 100% cpu routine or try to reproduce geiss ryan's hybrid routine.
coz as you already know, masm32's sleepex gives only 10ms precision,
but though a good example which is worth having a look at it, so thanx for pointing me to it.

i started coding something like a SetTimer replacement function which will then utilize a 1ms precise mechanism,
(but exactly this mechanism is currently missing). it using an extra thread.
i did this coz SetTimer is bound to using multiples of 10ms and 10ms is too fast for my needs and 20ms is way to slow.
so i need something which enables me using like 11,12,13,14,15,16,17,18, or 19ms, savvy?

hutch--

Unless you are running a very slow box, try the example with this change to it.

  ; change this,
    mov interval, 5                 ; set the timer interval here in milliseconds
  ; to this.
    mov interval, 1                 ; set the timer interval here in milliseconds


Here is the tail end of the output. In case you have problem with counting, this IS 1 millisecond resolution, savvy. You will find that smart arse wisecracks are not winners here.


950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000


Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

M4D45M

back again, after further testing,

as i changed the inerval it used 1ms precision BUT rendering 99% cpu.
for this i had to use the masm32 sleepex as you suggested coz the one you attached has
no interval setting.
i at first used the one you attached, and it had no inerval setting,
so i changed 'Sleep, 10, 0' to 'Sleep, 1, 0' which still gave 10ms precision,
which i think is due to 'Sleep' not really doing 1ms delay even if used in conjunction
with 'timeBeginPeriod, 1'. i think that's why sleepex renders 99% cpu.
it might just use it's own counter at full load instead of actually sleeping as 'SleepEx, 1, 0' always sleeps
9000us or 18000us on my machine even if i use 'timeBeginPeriod, 1' before!
i used the code timing macros to test this. so i can say that geiss ryan's tests prove the truth. this rhymes  :bdg



the fact that i can't change you, i have to change me, i guess, in accepting that you are absolute
and that i have to bow for everything you say. i think otherwize you'll kick me off this board.
but i also think that you could take someone other's advice/testing results seriously even if the person
is lower rank than you.
and i honestly always spend hours using yahoo and google and masm32board-search before i ask.
so most of the time other ppl in the wild hav the same problem / or also cannot figure something out properly.
thus those problems/circumstances actually exist in a way and are not a product of me being too stupid.
that r at least my humble thoughts.
i am not a smart arse wisecrack, but i hate that you NEVER believe me that something isn't working properly
coz you EVERYTIME expect ME to fail although the software DOES FAIL!
thus you are harsh to me coz you think i am way too stupid.

but thanx for the advice in using multimedia timers. with timeSetEvent i get very good results
which for 1 ms range from 960us to 980us. i could then use a *filler loop* like the one in sleepex.
so that simply does great! :U
thank you mate.




hutch--

madasm,

You are starting with the wrong set of assumptions in how you interact here. I am like most people who have been at this game for many years, i believe little but I do look at the testing and timings. No-one is absolute and if I have a claim to fame, it is that I have made more mistakes than most people have made in a number of lifetimes but I am equally good at fixing them.

When you start trying to win an argument when you have made obvious mistakes, people including me start to no longer take much notice of what you say because of it. One algo you complained about was debugged over 2 years ago and rewritten because of an errata yet you keep using stuff that old trying to rediscover the wheel when people with a lot more experience that you fixed the problem years ago.

Now where you did find a problem with a library module, instead of listening to what you were saying in the midst of a pile of mistakes, I put it in a test piece, duplicated the error then rewrote part of it so it worked properly. What you do if you find an error in code is report it as an errata with the conditions to duplicate it so it then can be fixed.

Now even with your response to the precision timer issue, you still did not use the version published in the masm32 example code that I referred you to. You have referenced the wrong code and ended up with the wrong conclusions. The example I posted above using the code  I referred you to does in fact deliver the 1 MS resolution that its output showed.

The general drift is if you respond to people who are trying to help you by trying to start arguments with them, they will no longer bother to help you.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Boucly

hutch and madasm, that was true but a bit harsh and too personally in my opinion, cool down a bit.

madasm, why do you need such precise timing and so much cpu power anyway?

Boucly

Mark Jones

It's ok. When I started, I used to think everything was a problem with MASM32 instead of me.

Quickly I realized that 99 times out of 100, the problem was MY code, and MASM32 was just fine.

If madasm sticks around long enough, he will probably realize this also. Here's to sticking around... :CHEERS:
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Boucly

Quote from: Mark Jones on August 18, 2006, 05:52:46 AM
It's ok. When I started, I used to think everything was a problem with MASM32 instead of me.

Quickly I realized that 99 times out of 100, the problem was MY code, and MASM32 was just fine.

If madasm sticks around long enough, he will probably realize this also. Here's to sticking around... :CHEERS:

Not to be nagging, but, I too had the same problem. Well, I was 100% sure that it was all my fault all the time because I got similar problems when I programmed in other languages previiously, but I get easily frustrated. And it wasn't easy to find help since I didn't have good internet connection or even know what forums are, plus I was twelve. I only relied on the help files. Now that I think about it, I am glad that I didn't. I helped me to learn what self-learning really is about. If someone was is in University, is that the kind of things I will have to go throgh?

Boucly