C
C#2w ago
Prof

✅ Async on single-threaded VM, mysterious speedup?

Consider the following code. This is running on a virtual machine with a single (!) core. How is it possible that on a single-threaded machine, the version where the tasks run in paralell is faster (see measurements in image)? The file is large (1.5MB). Thanks!
No description
No description
51 Replies
Prof
ProfOP2w ago
better image of the code
Prof
ProfOP2w ago
No description
Foffs
Foffs2w ago
I'm not sure if this will answer your question, but the operative system has something known as thread scheduling. It may pause a certain thread and take over another depending on the load and configuration
Prof
ProfOP2w ago
Sure, but in total, both cases have to do the same amount of work and there is only one thread available
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
it's a blank vm and I don't have shared clipboard how, though, there is only one CPU core?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
I don't understand this explanation there is only one thread, and the same total amount of work is done in both cases where does the faster runtime come from?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
what's the better way to benchmark that?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
Try BenchmarkDotNet if you want a sophisticated library for benchmarking https://github.com/dotnet/BenchmarkDotNet
Prof
ProfOP2w ago
IO doesn't require a working thread?
jcotton42
jcotton422w ago
$nothread
MODiX
MODiX2w ago
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
🤯 i thought that some bit of the CPU needs to work on the IO
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
is there anything in the standard library which can accurately time the execution of code? something like c++'s std::chrono::steady_clock?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
OH right the GC could screw my timing
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
it is mildly annoying that i need a third party library for reliable benchmarks oh well
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
That's not what I said, I'm just sad some fashion of reliable benchmarking is not included in the C# standard library(ies)
jcotton42
jcotton422w ago
Well, you can use Stopwatch to accurately time intervals.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
jcotton42
jcotton422w ago
But a proper benchmark needs things like JIT warmup.
Prof
ProfOP2w ago
I see though the most important thing to learn here is that parts of IO don't need the CPU's participation
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
ah yes as a firm c++ enjoyer I love footguns i digress
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
reading rn
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
you could say i'm asynchronously reading :kekw:
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
I like low-level, sure
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
Awesome thanks ❤️ I mean i've been wasting time writing a coroutine executor in C++ recently so this will definetely be helpful information
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Prof
ProfOP2w ago
alr this is :pog: ing information I shall continue reading Thanks!
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered
Prof
ProfOP2w ago
can i leave this thread around for just a little while to not lose track of the links?
Angius
Angius2w ago
All the close command does is adds a checkmark to the question to mark it as solved, it's not removed
Prof
ProfOP2w ago
ah good alr
Angius
Angius2w ago
That said, barely anybody closes their threads anyway, so... :KEKW:
Prof
ProfOP2w ago
:kekw:
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?