Jump to content

Wrong time being returned by the timer (Both Native Functions & Timer UDF)


 Share

Recommended Posts

Hello everyone :bye:

I am encountering a strange problem with timers... i.e wrong time difference :(. This is an test script made by me to compare Arrays & Maps in terms of speed, I wanted publish the results in my Maps topic, so I am hiding the question in the spoiler to prevent spoilage ;)

Ok, Here is the script which uses Timer UDF:

#include <MsgBoxConstants.au3>
#include <Timers.au3>

Global Const $SIZE = 10000 ; Initial size of the... er... variables (objects?)
Global Const $RESIZE = $SIZE + ($SIZE / 2) ; Size to resize the variables

$iTimer = _Timer_Init()

Local $aArray[$SIZE] ; Our array with $SIZE elements
MsgBox($MB_ICONINFORMATION, "Benchmark", "Making an array with " & $SIZE & " elements took ~" & _Timer_Diff($iTimer) / 1000 & " seconds.")

$iTimer = _Timer_Init() ; Reset the timer
Local $mMap[] ; Our Map

For $i = 1 To $SIZE ; Allocate our map with $SIZE elements
    $mMap[$i] = ""
Next
MsgBox($MB_ICONINFORMATION, "Benchmark", "Making a map with " & $SIZE & " elements took ~" & _Timer_Diff($iTimer) / 1000 & " seconds.")

$iTimer = _Timer_Init() ; Reset the timer
ReDim $aArray[$RESIZE] ; Resize out array
MsgBox($MB_ICONINFORMATION, "Benchmark", "Resizing an array with " & $SIZE & " elements to " & $RESIZE & " elements took ~" & _Timer_Diff($iTimer) / 1000 & " seconds.")

$iTimer = _Timer_Init() ; Reset the timer
For $i = $SIZE To $RESIZE ; Resize our map
    $mMap[$i] = ""
Next
MsgBox($MB_ICONINFORMATION, "Benchmark", "Resizing a map with " & $SIZE & " elements to " & $RESIZE & " elements took ~" & _Timer_Diff($iTimer) / 1000 & " seconds.")

Script with native Timer functions:

#include <MsgBoxConstants.au3>
#include <Timers.au3>

Global Const $SIZE = 10000 ; Initial size of the... er... variables (objects?)
Global Const $RESIZE = $SIZE + ($SIZE / 2) ; Size to resize the variables

$hTimer = TimerInit()

Local $aArray[$SIZE] ; Our array with $SIZE elements
MsgBox($MB_ICONINFORMATION, "Benchmark", "Making an array with " & $SIZE & " elements took ~" & TimerDiff($hTimer) / 1000 & " seconds.")

$hTimer = TimerInit() ; Reset the timer
Local $mMap[] ; Our Map

For $i = 1 To $SIZE ; Allocate our map with $SIZE elements
    $mMap[$i] = ""
Next
MsgBox($MB_ICONINFORMATION, "Benchmark", "Making a map with " & $SIZE & " elements took ~" & TimerDiff($hTimer) / 1000 & " seconds.")

$hTimer = TimerInit() ; Reset the timer
ReDim $aArray[$RESIZE] ; Resize out array
MsgBox($MB_ICONINFORMATION, "Benchmark", "Resizing an array with " & $SIZE & " elements to " & $RESIZE & " elements took ~" & TimerDiff($hTimer) / 1000 & " seconds.")

$hTimer = TimerInit() ; Reset the timer
For $i = $SIZE To $RESIZE ; Resize our map
    $mMap[$i] = ""
Next
MsgBox($MB_ICONINFORMATION, "Benchmark", "Resizing a map with " & $SIZE & " elements to " & $RESIZE & " elements took ~" & TimerDiff($hTimer) / 1000 & " seconds.")

If you run the script(s), you can see that sometimes the time difference is >1 but the actual difference is almost instantaneous, I don't know what is causing the problem, I think the problem is in my end this time because I used both native and UDF functions and they return identical results... However I spotted something interesting in the documentation for TimerDiff:

"Remarks: Some processors are known to cause the function to return incorrect result."

Hmmm.... is my processor the culprit? Or is something wrong in my script...

Thanks in Advance! TD :D

 

P.S It was hard to write in the dark spoiler :P 

Edited by TheDcoder
A quote was remove from the spoiler, added the quote in "quotes" instead :P & Added Tags

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Sometimes the result is using exponents, so the >1 actually isn't greater than one, it's actually much less.

Making an array with 10000 elements took ~4.14466376513888e-005 seconds.

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH :o, I don't understand scientific notation very well :P. I tried to Round the result down to five decimals but...

5122b1ee242b63568207e06f3cbe3b82.png

Its still not calculating the scientific notation :ermm:. Thanks! :D

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

It might be simpler to count how many loops you can do in x time instead of counting how long it takes to do x many loops.

Either way, I'm not sure what you're doing makes sense? You are not really comparing the same thing. You simply create and resize the array but you're creating and filling the map with empty strings. What useful is a comparison that compares 2 completely different things?

 

Edited by AdmiralAlkex
Clarified second half...
Link to comment
Share on other sites

@AdmiralAlkex I know that its not a fair test as the Map is not made for these kind of tasks, I just wanted to see if its miraculous and is close to arrays even in a unfair test :)

 

P.S I am not going to publish it anymore because the test failed.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@BrewManNH :o, I don't understand scientific notation very well :P. I tried to Round the result down to five decimals but...

5122b1ee242b63568207e06f3cbe3b82.png

In this case the number, without using scientific notation, would be .00003763 seconds the e-005 tells you to move the decimal point 5 places to the left.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...