Optimize this code
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
well, this code totally sucks, not sure how you could hope to improve it apart from scrapping it. You could write a generic version, but I don't think generics in .NET support specialisation. How about abandoning it ?
Christian Graus Driven to the arms of OSX by Vista.
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
Also, what happens when from is an int and to is a DateTime?
Cheers, Vıkram.
I've never ever worked anywhere where there has not been someone who given the choice I would not work with again. It's a job, you do your work, put up with the people you don't like, accept there are probably people there that don't like you a lot, and look forward to the weekends. - Josh Gray.
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
Instead of "
if (typeof(X) == from.GetType())
", use "if (from is X)
". But it would be better if you redesign the program so that you don't have to test the type at all. -
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
The only way to do something like this 'properly' is using generics. This however creates all sorts of problems as doing math on T is not allowed for obvious reasons. Have a look at http://www.codeproject.com/KB/cs/genericnumerics.aspx[^] article - it may help.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
private static double CalculateDifferance(object from, object to)
{
Type t = typeof(from);
// are you confident that both types match?
// if(!(to is t))
// throw new InvalidOperationException("from and to must be of the same type!");switch (t)
{
case System.Int32:
return (int)to - (int)from;
break;
case System.DateTime:
return ((DateTime)to - (DateTime)from).TotalSeconds;
break;
case System.Double:
return (double)to - (double)from;
break;
default:
return double.NaN;
break;
}
}Unless you have a way of indicating the types of the incoming parameters you will have to do the type-checks and unboxing which will be the biggest performance hits. I'd recommend reviewing the calls into the function and see if you know at that time whether you're dealing with int, DateTime, or double. If so, break it out into 3 separate functions that are type-specific.
Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
-
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
public static double CalculateDifference(double to, double from){
return to-from;
}
public static int CalculateDifference(int to, int from){
return to - from;
}etc.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego. -
I have a function that very often are called. Any tip on how to optimize this code:
private static double CalculateDifferance(object from, object to)
{
if (typeof(int) == from.GetType())
{
return (int)to - (int)from;
}
else if (typeof(DateTime) == from.GetType())
{
return ((DateTime)to - (DateTime)from).TotalSeconds;
}
else if(typeof(double) == from.GetType())
{
return (double)to - (double)from;
}
return double.NaN;
}_____________________________ ...and justice for all
Edited to get < and > to display! This works - using an interface/generics.
public interface IMath<T> { double CalculateDifference(T from, T to); } public class TestMath : IMath<int>, IMath<double>, IMath<datetime> { #region IMath<int> Members public double CalculateDifference(int from, int to) { return to - from; } #endregion #region IMath<double> Members public double CalculateDifference(double from, double to) { return to - from; } #endregion #region IMath<datetime> Members public double CalculateDifference(DateTime from, DateTime to) { return (to - from).TotalSeconds; } #endregion }
TestMath testMath = new TestMath();
Console.WriteLine(testMath.CalculateDifference(DateTime.Now, DateTime.Now.AddDays(1)));Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)modified on Friday, February 6, 2009 9:40 AM