✅ :white_check_mark: Dictionary<int, ...> lookup time seems a bit too slow
I was profiling a program, and I saw this:
Here is the code in question:
136ms for 190,000 calls is translates to about 1,400 lookups per millisecond. That seems a little slow to me, is it not? If it is, why could this be? I figured looking up an int in a hashmap should be faster than that.
13 Replies
just to be clear, this was measured in release mode, right?
This was done with a line-by-line profiler, the configuration is set to Release mode and I used the option highlighted in the screenshot.
then i would expect this to be at least 10x faster
What could be the reason for it to be so slow? Maybe I should just chalk this up to line-by-line profiling miscalculations for now
I don't know what line by line profiling is, but doing 1 million lookups on my machine took 5 milliseconds.
Just measured with Stopwatch.
I don't know how line-by-line works under the hood, but if it's instrumenting the code I'd expect that to add a small overhead. When compared to something as cheap as a dictionary lookup, that overhead may well be significant
This is what I got from a quick benchmark, that has a pre existing dictionary/hashset and tries to get a random number from it n times.
i agree that profiling is not going to be very helpful for measuring the absolute performance of a line or two of code
it's much more helpful for measuring relative performance, how much longer you spend in some function compared to all the rest. the distortion caused by the profiling applies about equally, so it balances out
if you want a microbenchmark to compare things precisely, you should use a library like BenchmarkDotNet (as the previous post did)
Thanks guys!
I don't get to use line-by-line profilers too often, so maybe I'm just overestimating how well it accounts for its own overhead in the benchmark results
I've gotten the performance to an acceptable threshold for now by optimizing other things, if it falls under the threshold again I'll try using BenchmarkDotNet to see if there is really some issue with the dictionary/method or if it was just the overhead after all
I suspect it doesn't account for overhead. It's meant to show you which of your lines are more expensive compared to other lines
Yeah, that's fair
$close
Use the /close command to mark a forum thread as answered