سؤال

I am working on a library that needs to do some date math extremely quickly. I am using Jon Skeet's Noda Time library, which uses Tick math, and so is extremely quick, but I want to get elapsed seconds in a way faster than casting a Noda Time Instant or Duration to a TimeSpan.

The fastest way I have been able to do it so far is using Ticks * 1E-07, but I am thinking that bit shifting might work faster (since it will always be 1E-07). Thanks in advance for any thoughts!

هل كانت مفيدة؟

المحلول

Don't cast to TimeSpan - just take the Ticks and divide by NodaConstants.TicksPerSecond. It's an integer division, and likely to be very, very quick. Given that 107 isn't a power of two (or particularly neatly represented as combinations of them) I suspect you'd be best off just dividing.

I'd be surprised if this division really proved to be a bottleneck in your code. While I'm trying to keep Noda Time quick, I'm not above performing a division or two where appropriate :)

If you need partial seconds, I'd be tempted to just divide by TicksPerMillisecond instead, and use an integer number of milliseconds rather than using floating-point arithmetic. If you already need a double elsewhere, of course, that's not going to help much.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top