Jump to content
ripdad

Trying to get a function to call in nanoseconds efficiently

Recommended Posts

Posted (edited)

Before I get started, I'm aware of "NtDelayExecution". It doesn't work very good for me.

But this does...

For $i = 1 To 100
    Sleep(0)
Next

I'm trying to delay a call to a function in nanoseconds with another function in a loop.

My question: Is there a safe and efficient function to do this with?

Sleep(0) seems to work, but I wondered if someone might know of a better function or perhaps another way.

Thanks for any constructive input or thoughts!

--Edit--

My target is about 250ns.

Anywhere in there would be fine.

 

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Posted (edited)

Hmm, i wrote a sleeper function, for this post: Mouse Recorder , to be able to access the tray menue while using sleep/delay.

Maybe you could use it instead of sleep ?
1000 for the $nr should be 1 second. 

Func Sleeper($nr)
    Local $varTS = _Timer_Init()
    While _Timer_Diff($varTS) < $nr
        ;CheckTray()
    WEnd
EndFunc   ;==>Sleeper

 

Edited by Dan_555

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

Share this post


Link to post
Share on other sites
55 minutes ago, ripdad said:

My target is about 250ns.

Given that AutoIt is an interpreted language, I seriously doubt that you can get anywhere near a 250ns pause.  On a 64-bit Windows 7 Pro OS, with an Intel i5 @ 3.1ghz, and no other apps running (only services), the following lines yield around a 4500ns diff.  That is just the time it takes to initialize a timer and then do an immediate time difference.  So in a loop, and depending on what else is running on the PC, the results would be an even bigger difference.  Another way to look at is that you are most likely seeing a bigger pause than 250ns when you execute your commands back to back, without any Sleep function.

$hTimer = TimerInit()
$nDiff = TimerDiff($hTimer)
MsgBox(0, "", "msecs = " & $nDiff & @CRLF & "nsecs = " & ($nDiff * 1000000))

 

Share this post


Link to post
Share on other sites

Read this page: https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps

Yet beware that the overhead implied by at least two AutoIt calls to any API could waste much more time than the period you aim to tick. Maybe devolve such task to machine code, inline or from a DLL.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks for all your reply's. I really do appreciate it.

I fear that reading any value with consolewrite or msgbox would slow it down even further.

I have been playing with it some more and found that this is sufficient enough for me.

For $i = 1 To 2
Next

Although, I have no idea what the reading would be without a literal read.

But, it works good enough. That's all that matters. Thanks again!

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

ripdad,

This thread might be of interest for you:

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thanks Melba23. I did try ZwDelayExecution and NtDelayExecution. I didn't have good luck with either of them.

Not sure why at this point

---

I played with it some more and found that time is less a factor than another variable.

So, what I wanted was a way to expand an audio waveform live in as close to realtime as I could.

I knew I couldn't achieve realtime.

This graphic is as close as I could come to it without losing coherency of it...

graphic_waveform.png.e93081605b288225c3399d134c3ddb84.png

I had a very hard time taking a screenshot of it. It was going by so fast!

This graphic may not mean much to most people, but to me, this is amazing to see it LIVE.

Every musical note with its time duration. And the detail is astounding.

---

There's an old saying: "there a hole in everything" -- And I found the hole that I was running into by accident.

Do you think music has holes in it? Well, not literal holes -- rather silence. In other words, zero volume.

And It does. More than most people know. This silence is what was tripping me up.

Why does music have silence? Two factors -- 1. natural and 2. unnatural.

These days, everything is digitally encoded. If you understand how they achieve that, then you'll understand what I'm saying.

I'm not going explain it here -- it would take too long.

---

What I wound up doing is cutting the bottom 3% of the volume out, which gets lost in the envelope anyways.

And what do you know? I works!

It does come at a CPU cost of about 50%. But well worth seeing.

In the end, the function call is in a While loop with nothing else. Time is whatever it takes to complete the waveform function.

Thanks everyone and have a good day!

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Posted (edited)

This graphic shows what it looks like with the silence left in it at that speed.

graphic_waveform2.png.709eaab621dc67707c7146a502d431ab.png

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...