本文介绍了我怎样才能实现与System.TimeSpan值的模操作,无需循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code(C#/ WPF)非常性能敏感的部分,我需要以最快的方式二System.TimeSpan值之间进行模运算。

I'm in a very performance-sensitive portion of my code (C#/WPF), and I need to perform a modulus operation between two System.TimeSpan values in the quickest way possible.

这code将运行每秒数千次,我非常preFER避免使用手动回路计算 - 不惜一切代价。

This code will be running thousands of times per second, and I would very much prefer to avoid using a manual loop calculation - at all costs.

双时间跨度之间的模量的想法似乎有点怪异,所以让我来解释 -
说我们有
时间跨度A = 1分30秒
时间跨度B = 20秒

The idea of a modulus between two TimeSpans may seem a little weird, so allow me to explain -
Say we have
TimeSpan A = 1 Minute 30 Seconds
TimeSpan B = 20 Seconds

下面是常见的操作及其合理结果的列表:

Here would be a list of common operations and their reasonable results:

A + B =(时间跨度)1分50秒

A + B = (TimeSpan)1 Minute 50 Seconds

A - B =(时间跨度)1分10秒

A - B = (TimeSpan)1 Minute 10 Seconds

A * B =没有合理的方式来计算
我们应该能够的一个整数乘以一个时间跨度。 A * 5 =(时间跨度)7分30秒
微软并没有实施时间跨度和整数之间的乘法。

A * B = No Reasonable Way to Compute
We should be able to multiply a TimeSpan by an integer. A * 5 = (TimeSpan) 7 Minutes 30 Seconds
Microsoft has not implemented multiplication between TimeSpans and integers.

A / B =(INT)4或(双)4.5
在.NET框架此操作不直接实现,但它是非常合情合理的。
有4.5 B的A中(4.5 * 20 = 90)

A / B = (int)4 or (double)4.5
This operation is not implemented directly in the .NET framework, but it makes perfect sense.
There are 4.5 B's in A. (4.5 * 20 = 90)

A%B =(时间跨度)10秒
由于时间跨度合理分工,时间跨度模量应为pretty的直线前进。
A / B 真的等于(INT)4 (时间跨度)10秒。商和余数是不同的数据类型,它实际上可能是为什么微软不直接实现这一点。

A % B = (TimeSpan) 10 Seconds
Given reasonable TimeSpan division, TimeSpan modulus should be pretty straight-forward.
A / B really equals (int)4 remainder (TimeSpan)10 Seconds. The quotient and remainder are different data types, which may in fact be why Microsoft hasn't implemented this directly.

我需要找到计算这个不用循环的有效途径。通常情况下我也不会反对短环,但这些时间跨度可能有很大的不同。较大的时间跨度之间的指数差,较大的商数。该商越大,越迭代分环,将必须执行。这是一种依赖,我不能让这部分我的应用程序。

I need to find an efficient way to compute this without looping. Normally I wouldn't be opposed to a short loop, but these TimeSpans could differ greatly. The larger the exponential difference between the TimeSpans, the larger the quotient. The larger the quotient, the more iterations a "divide-loop" will have to execute. This is a dependency that I cannot allow in this part of my app.

这样做有什么想法?

推荐答案

乘法很简单:

TimeSpan a5 = TimeSpan.FromTicks(A.Ticks * 5);

同样,A / B:

Likewise A/B:

double aOverB = (double)A.Ticks / B.Ticks;

和A%B:

TimeSpan aModB = TimeSpan.FromTicks(A.Ticks % B.Ticks);

演示:

using System;

class Test
{
    static void Main()
    {
        TimeSpan a = TimeSpan.FromSeconds(90);
        TimeSpan b = TimeSpan.FromSeconds(20);

        TimeSpan a5 = TimeSpan.FromTicks(a.Ticks * 5);
        double aOverB = (double)a.Ticks / b.Ticks;
        TimeSpan aModB = TimeSpan.FromTicks(a.Ticks % b.Ticks);

        Console.WriteLine(a5);
        Console.WriteLine(aOverB);
        Console.WriteLine(aModB);
    }
}

输出:

00:07:30
4.5
00:00:10

这篇关于我怎样才能实现与System.TimeSpan值的模操作,无需循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:51