문제

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