Which is more computationally expensive, trying to acquire a read/write lock or doing a few datetime comparisons?
-
I have the following options in designing a block of code. In this design, we do a comparison and only if it's true we try acquiring the lock. if((DateTime.Now - timeStamp) < new TimeSpan(24,0,0)){ if(rwlock.tryWrite(4000)){ // blocks for 4s .... } } In this design, we first try acquiring the lock and only if we can we do the comparison. if(rwlock.tryWrite(4000)){ // blocks for 4s if((DateTime.Now - timeStamp) < new TimeSpan(24,0,0)){ .... } } I think it's best to put the less computationally expensive check first. So which check is less computationally expensive?
-
Answer:
Go for the first solution, for sure. Acquiring a lock implies much more work than a small bench of arithmetic operations. Moreover, if you have an heavy load, you'll avoid as contention as your test on dates is selective.
Marc Dousset at Quora Visit the source
Related Q & A:
- Does the Linux Professional Institute Certification (LPIC) program have an advantage in trying to find a job?Best solution by Yahoo! Answers
- How can you lock folders with a password in Vista?Best solution by Yahoo! Answers
- I'm trying to get a meeting with a diplomat to counsel me on a career in international affairs?Best solution by Yahoo! Answers
- I was trying to send a e-mail and it did not send how can i find it again.Best solution by Yahoo! Answers
- Trying to find a smaller printer.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.