Well, in this case, I figured that even if I ran it a million times, I would only save two seconds, hardly enough time to really enjoy that additional gulp of beer…
I added EpsilonEquals to RhinoCommon a while back, and its performance is slightly slower due to the fact that it takes NaN an Infinity into account, as well as floating point representation of numbers whose difference is not well defined due to the details of how they are represented in bits.
I am actually surprised that the difference is so small
do you get different results too when running your script multiple times ? Mine vary between 0.8 and 1 second. It seems that it runs both tests slightly faster by using xrange instead:
I´ve been curious how it compares to vbScript using the code below (not shure if both scripts can be called equivalent):
Option Explicit
Call speedtestcompare()
Sub speedtestcompare()
Dim numbers(5000000), i, dbltol, start_time, x
For i = 0 To 5000000 : numbers(i) = rnd * i : Next
dbltol = 0.01
start_time = timer
For i = 1 To 5000000
If Abs(numbers(i - 1) - numbers(i)) < dbltol Then
x = True
End If
Next
Rhino.Print "vbScript: " & timer - start_time
End Sub
The result was 1.875 seconds. Can anyone explain why there is such a large difference compared to python or suggest ways to gain more speed?
Yes, I’ve noticed this too - on certain things Python is way slower than VBS. I don’t know if that’s because it has to go through an additional layer of stuff or what…
For your reference, this is the code that is in RhinoMath.EpsilonEquals.
/// <summary>
/// Compare two doubles for equality within some "epsilon" range
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="epsilon"></param>
/// <returns></returns>
public static bool EpsilonEquals(double x, double y, double epsilon)
{
// IEEE standard says that any comparison between NaN should return false;
if (double.IsNaN(x) || double.IsNaN(y))
return false;
if (double.IsPositiveInfinity(x))
return double.IsPositiveInfinity(y);
if (double.IsNegativeInfinity(x))
return double.IsNegativeInfinity(y);
// if both are smaller than epsilon, their difference may not be.
// therefore compare in absolute sense
if (Math.Abs(x) < epsilon && Math.Abs(y) < epsilon)
{
bool result = Math.Abs(x - y) < epsilon;
return result;
}
return (x >= y - epsilon && x <= y + epsilon);
}