The MASM Forum

General => The Workshop => Topic started by: Gunther on February 04, 2021, 07:35:22 PM

Title: Timer Macros
Post by: Gunther on February 04, 2021, 07:35:22 PM
Under MASM32 SDK we had nifty timer macros (I think Michael W. wrote those). Is there something comparable for Win64?

Gunther
Title: Re: Timer Macros
Post by: jj2007 on February 04, 2021, 10:00:42 PM
Use QueryPerformanceCounter, it's the best option
Title: Re: Timer Macros
Post by: TimoVJL on February 04, 2021, 10:23:41 PM
A useless loop before timing should wake up CPU to full speed.
A one AMD was running at speed 800 MHz before tests.
Title: Re: Timer Macros
Post by: hutch-- on February 04, 2021, 11:37:16 PM
Timo is right, my old i7 used to idle at 1.2 gig and under load 3.3 gig and it was not until I clocked it to a higher frequency that it stabilised. The trick was to run the code once then clock the rest. Michael's code used to work well but when CPUs started to run variable clock speeds, especially on different cores it no longer worked well.
Title: Re: Timer Macros
Post by: jj2007 on February 04, 2021, 11:54:25 PM
Counting cycles rarely makes sense. The M$ recommended way to time algos is QueryPerformanceCounter:

include \Masm32\MasmBasic\Res\JBasic.inc ; ## builds in 32- or 64-bit mode with UAsm, ML, AsmC ##
MbNanoStart QWORD ?
MbNanoEnd QWORD ?
MbNanoSecs QWORD ?
MbNanoFreq QWORD ?
Init ; OPT_64 1 ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This program was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format.")
  jinvoke QueryPerformanceCounter, offset MbNanoStart
  jinvoke Sleep, 1000
  jinvoke QueryPerformanceCounter, offset MbNanoEnd
  fild MbNanoEnd
  fild MbNanoStart
  fsub
  fld st
  jinvoke QueryPerformanceFrequency, offset MbNanoFreq
  fild MbNanoFreq
  fdiv
  fmul FP4(1000.0)
  fistp MbNanoSecs
  Inkey Str$("The Sleep(1000) took %lu milliseconds", MbNanoSecs)
EndOfCode


Output:
This program was assembled with ml64 in 64-bit format.
The Sleep(1000) took 999 milliseconds
Title: Re: Timer Macros
Post by: mikeburr on February 05, 2021, 08:03:50 AM
0F31 from memory
regards mike b
Title: Re: Timer Macros
Post by: Gunther on February 05, 2021, 02:06:07 PM
Thank you for all the advice given.  :thumbsup: I'll think about it and believe that I can solve my problem.

Gunther