C# huge performance drop assigning float value
c#, performance
Solution
I'll post another theory: it might be the cache miss of the first access to members of `td`. A memory load takes 100-200 cycles which in this case seems to amount to about 1/3 of the total duration of the method.
Points to test this theory:
- Is your data set big? It bet it is.
- Are you accessing the `TagData`'s in random memory order? I bet they are not sequential in memory. This causes the memory prefetcher of the CPU to be dysfunctional.
- Add a new line `int dummy = td.tf;` before the expensive line. This new line will now be the most expensive line because it will trigger the cache miss. Find some way to do a dummy load operation that the JIT does not optimize out. Maybe add all `td.tf` values to a local and pass that value to `GC.KeepAlive` at the end of the method. That should keep the memory load in the JIT-emitted x86.
I might be wrong but contrary to the other theories so far mine is testable.
Try making `TagData` a `struct`. That will make all items of `term.tags` sequential in memory and give you a nice performance boost.
Problem
I am trying to optimize my code and was running VS performance monitor on it. It shows that simple assignment of float takes up a major chunk of computing power?? I don't understand how is that possible. Here is the code for TagData: ``` public class TagData { public int tf; public float tf_idf; } ``` So all I am really doing is: ``` float tag_tfidf = td.tf_idf; ``` I am confused.