On SMP machines is there a performance benefit to #2 vs #1:
1) x = 0;
or
2) if (x) x = 0;
I was thinking that the behind the scenes overhead to manage cache coherency between the CPUs might have some cost. Am I nuts?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Even with single-threaded code, the latter can have an advantage if the object lies in copy-on-write memory (for example, a private mapping of a file, or almost any writable memory after a fork). I suspect the advantage you’re asking about is real too, at least on systems like x86 where memory consistency is handled automatically. On such machines, writing to memory that might be in another cpu’s cache will invalidate the cached copy (actually the entire cache line). Just reading won’t do any harm. Of course, if this is memory that’s potentially being modified and shared by multiple threads, it needs to be protected by synchronization mechanisms anyway, and then you probably lose most or all of the advantage.