CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    381

    Optimize this code

    I have a function that very often are called. Any tip on how to optimize this code:

    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

  2. #2
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: Optimize this code

    Quick idea - make overloaded function for each type instead of checking type and casting.

  3. #3
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Optimize this code

    Or you could use generics and a custom interface and have the method instill a generic constraint such as:

    Code:
    public static T CalculateDifference<T>(T numOne, T numTwo) where T : ISubtractable
    {
       // Perform subtractions here
    }
    And have ISubtractable overload the "-" operator.
    But then you end up needing to build a wrapper class for your numerical values and implement the ISubtractable interface... so I guess its not all that "elegant" in the end.
    Last edited by RaleTheBlade; February 7th, 2009 at 01:49 PM. Reason: Code Tags
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Optimize this code

    in general from the aspect of OOP programming when you use many typeof and casting in your function is the symptom that you need to use polymorphism.

    BTW in your case you don't need optimize you need re-design. you may understand what will be happen in your method when you pass Object to it but assume that you work in a team and you provide interface to another programmer how do you expect he understand your function prototype intuitively.

    it would be better that you do the hard job yourself and make job of others easy. so overload the function for each type or use Generics

    in other hand it seems that your function is not needed at all.
    for those types that intrinsically support subtracting there is no need to your function and for those that do not support you can use operator overloading that is more elegant than having such function you have wrote.
    Last edited by toraj58; February 9th, 2009 at 11:28 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured