How to convert full date/time to timestamp?

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

Was this solution helpful to you?

Find solution

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.