Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Optimize this code

Optimize this code

Scheduled Pinned Locked Moved C#
tutorialcode-review
9 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anderslundsgard
    wrote on last edited by
    #1

    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

    C V K D D 8 Replies Last reply
    0
    • A anderslundsgard

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A anderslundsgard

        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

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A anderslundsgard

          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

          K Offline
          K Offline
          kaminem
          wrote on last edited by
          #4

          Use Convert.ToDouble and Convert.ToDateTime

          1 Reply Last reply
          0
          • A anderslundsgard

            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

            D Offline
            D Offline
            Daniel Grunwald
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A anderslundsgard

              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

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              • A anderslundsgard

                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

                B Offline
                B Offline
                Ben Fair
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • A anderslundsgard

                  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

                  E Offline
                  E Offline
                  Ennis Ray Lynch Jr
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • A anderslundsgard

                    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

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups