Which is generally more efficient between locking a mutex and doing disk reads in C++?
If it depends on the system I’m running, what’s a good way to check this? Would doing 1 million locks vs 1 million disk reads work?
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.
Generally, the mutex operations would be far faster than disk reads. That’s because their intended purpose requires maximum speed. In fact, I would hazard to suggest that any memory operation is likely to be faster than a disk one.
Of course, there may be bizarre edge cases where the disk information is cached in memory and
you read the same byte over and over but that’s not really doing a disk read, is it?
If you find yourself with a desire to check this (and I would suggest it’s not really necessary), simply code up a couple of programs, the first which locks and unlocks a mutex a million times, the second which reads a million different pieces of information off the disk.
Run them and compare them. If, by some bizarre factor, the disk reads come back faster, show us your code and we’ll explain what you did wrong 🙂