Skip to content

Why Dictionary is faster with structs than with classes? #100306

Answered by alexeyfv
alexeyfv asked this question in Q&A
Discussion options

You must be logged in to vote

I've found the root cause of the problem. @elgonzo @huoyaoyuan you both were right - it is related to data locality and cache. :)

I added hardware counters to the benchmark to see cache misses. Here are the results that I got:

| Method   | Mean      | CacheMisses/Op |
|--------- |----------:|---------------:|
| Class1   |  26.89 ms |        771,546 |
| Class5   |  34.98 ms |      1,434,310 |
| Class10  |  46.31 ms |      2,177,061 |
| Class15  |  56.28 ms |      2,571,176 |
| Class20  |  63.04 ms |      3,002,823 |
| Class25  |  70.86 ms |      3,313,459 |
| Struct1  |  18.33 ms |         85,184 |
| Struct5  |  21.53 ms |        125,696 |
| Struct10 |  27.00 ms |        190,490 |
| Struct…

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@alexeyfv
Comment options

@alexeyfv
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by alexeyfv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants