我有这个功能:
int round(double val) {
if (val >= 0) {
return (int)Math.Floor(val + 0.5);
}
return (int)Math.Ceiling(val - 0.5);
}
我在我的程序中多次调用它,我的意思是很多次,所以每个毫秒的运行时间都很重要。有没有办法让它比现在更快? THX
编辑:
该函数是用于计算图像中线条的切线方向的算法的一部分。它取自学术文章。由于它以弧度值处理角度,因此它使用小而精确的数字。
I / O示例:
0 -> 0
1 -> 1
1.1 -> 1
1.51 -> 2
-0.1 -> 0
-1 -> -1
-1.1 -> -1
-1.51 -> -2
EDIT2:
根据评论,我将检查的功能更改为:
int round(double val) {
return (int)Math.Round(val, MidpointRounding.AwayFromZero);
}
更新的问题是:Math.Round函数是最快的舍入方式吗?
答案 0 :(得分:2)
你可以加快速度。这要快很多倍:
if (val >= 0)
{
return (int)(val + 0.5d);
}
return = (int)(val - 0.5d);
你避免使用所有这些数学库的东西。问题是,它真的重要吗?对于1500000次转换,您第一次使用的时间是18ms。您的EDIT2功能是36ms。此功能是4ms。
根据这个测量,处理器可以比较两个双打,添加两个双打并在大约2.5ns内转换一个。但如果它没有在缓存中,从主存储器读取可能需要100ns。衡量有时可能会产生误导。
这是完整的代码
#region stopky
public class Stopky
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long frequency);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long ticks);
protected static double frequency = -1;
public void setStart()
{
QueryPerformanceCounter(out tickStart);
}
public double getTimeFromStart
{
get
{
QueryPerformanceCounter(out tickNow);
double time = (tickNow - tickStart) / frequency;
return time;
}
}
private long tickStart;
private long tickNow;
public Stopky()
{
if (frequency < 0)
{
long tmp;
QueryPerformanceFrequency(out tmp);
if (tmp == 0)
{
throw new NotSupportedException("Error while querying "
+ "the high-resolution performance counter.");
}
frequency = tmp;
}
setStart();
}
public void Show()
{
MessageBox.Show(this.getTimeFromStart.ToString());
}
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
double[] examples = new double[] { 0, 1, 1.1, 1.51, -0.1, -1, -1.1, -1.51 };
int totalCount = 1500000;
double[] examplesExpanded = new double[totalCount];
for (int i = 0, j = 0; i < examplesExpanded.Length; ++i)
{
examplesExpanded[i] = examples[j];
if (++j >= examples.Length) { j = 0; }
}
int[] result1 = new int[totalCount];
int[] result2 = new int[totalCount];
int[] result3 = new int[totalCount];
Stopky st = new Stopky();
for (int i = 0; i < examplesExpanded.Length; ++i)
{
result1[i] = (int)Math.Round(examplesExpanded[i], MidpointRounding.AwayFromZero);
}
st.Show();
st = new Stopky();
for (int i = 0; i < examplesExpanded.Length; ++i)
{
double val = examplesExpanded[i];
if (val >= 0)
{
result2[i] = (int)Math.Floor(val + 0.5);
}
result2[i] = (int)Math.Ceiling(val - 0.5);
}
st.Show();
st = new Stopky();
for (int i = 0; i < examplesExpanded.Length; ++i)
{
double val = examplesExpanded[i];
if (val >= 0)
{
result3[i] = (int)(val + 0.5d);
}
else
{
result3[i] = (int)(val - 0.5d);
}
}
st.Show();
for (int i = 0; i < totalCount; ++i)
{
if(result1[i] != result2[i] || result1[i] != result3[i])
{
MessageBox.Show("ERROR");
}
}
MessageBox.Show("OK");
}
一些注释
答案 1 :(得分:0)
为什么不使用内置的Math.Round
方法?
int round(double val) {
if (val >= 0) {
return Math.Round(val, MidpointRounding.AwayFromZero);
}
return Math.Round(val, MidpointRounding.ToEven);
}
https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx