about generics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    about generics

    Hello!

    I have read that in practice, casting proved to be several times faster than
    using a generic.

    So the main reason to use generics is not that the performance is better
    because that's not the case.
    The only reason is that it's type-safe.

    I must ask if anyone has made any significant performance improvement using
    generics ?

    //Tony


  • Jon Skeet [C# MVP]

    #2
    Re: about generics

    Tony Johansson <johansson.ande rsson@telia.com wrote:
    I have read that in practice, casting proved to be several times
    faster than using a generic.
    Where have you read that? It's such a vague statement that it can't
    really be commented on without a specific example.
    So the main reason to use generics is not that the performance is better
    because that's not the case.
    Yes it is - particularly with value types.
    The only reason is that it's type-safe.
    It's also far more expressive in terms of API, cleaner to use, and
    avoids boxing/unboxing.
    I must ask if anyone has made any significant performance improvement using
    generics ?
    Anyone who uses value types in a collection for a start...

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Marc Gravell

      #3
      Re: about generics

      Generics also avoid boxing, which can (in some scenarios) be an
      overhead. But generics avoid the need to cast, so can improve
      performance.

      In some scenarios use generics extensively to optimise some reflection
      operations, along the same lines as Jon's blog here:



      Marc

      Comment

      • Tony Johansson

        #4
        Re: about generics

        Hello!

        This is what Tony Northrup MCSE CISSP and Microsoft MVP says.
        "I haven't been able to reproduce the performance benefits of generics;
        however,
        according to Microsoft, generics are faster than using casting. In practice,
        casting proved to be several times faster than
        using a generic. However, you probably won't notice performance difference
        in your application.
        (My Test over 100 000 iterations took only a few seconds). So you should
        still use generics because they are type-safe."

        //Tony

        "Marc Gravell" <marc.gravell@g mail.comskrev i meddelandet
        news:65014859-2061-4f8b-bc36-8443b0553c09@y2 1g2000hsf.googl egroups.com...
        Generics also avoid boxing, which can (in some scenarios) be an
        overhead. But generics avoid the need to cast, so can improve
        performance.
        >
        In some scenarios use generics extensively to optimise some reflection
        operations, along the same lines as Jon's blog here:
        >

        >
        Marc

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: about generics

          Tony Johansson <johansson.ande rsson@telia.com wrote:
          This is what Tony Northrup MCSE CISSP and Microsoft MVP says. "I
          haven't been able to reproduce the performance benefits of generics;
          however, according to Microsoft, generics are faster than using
          casting. In practice, casting proved to be several times faster than
          using a generic. However, you probably won't notice performance
          difference in your application. (My Test over 100 000 iterations took
          only a few seconds). So you should still use generics because they
          are type-safe."
          That's pretty meaningless as he doesn't say what he's doing. Is there
          really no code provided or even a description of his test?

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • Tony Johansson

            #6
            Re: about generics

            Hello!

            No

            //Tony


            "Jon Skeet [C# MVP]" <skeet@pobox.co mskrev i meddelandet
            news:MPG.234362 a376d6edacf72@m snews.microsoft .com...
            Tony Johansson <johansson.ande rsson@telia.com wrote:
            >This is what Tony Northrup MCSE CISSP and Microsoft MVP says. "I
            >haven't been able to reproduce the performance benefits of generics;
            >however, according to Microsoft, generics are faster than using
            >casting. In practice, casting proved to be several times faster than
            >using a generic. However, you probably won't notice performance
            >difference in your application. (My Test over 100 000 iterations took
            >only a few seconds). So you should still use generics because they
            >are type-safe."
            >
            That's pretty meaningless as he doesn't say what he's doing. Is there
            really no code provided or even a description of his test?
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: about generics

              On Sep 23, 2:42 pm, "Tony Johansson" <johansson.ande rs...@telia.com >
              wrote:
              Hello!
              >
              I have read that in practice, casting proved to be several times faster than
              using a generic.
              What if you do not know (or care) to what to cast to?
              also I bet you anything that it will nt apply to a value type, say
              Int32
              So the main reason to use generics is not that the performance is better
              because that's not the case.
              The only reason is that it's type-safe.
              As I said a cast with a value type is a slow operation, you have to
              unbox it.
              I must ask if anyone has made any significant performance improvement using
              generics ?
              >
              //Tony

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: about generics

                On Sep 23, 3:56 pm, "Tony Johansson" <johansson.ande rs...@telia.com >
                wrote:
                Hello!
                >
                This is what Tony Northrup MCSE CISSP and Microsoft MVP says.
                "I haven't been able to reproduce the performance benefits of generics;
                however,
                according to Microsoft, generics are faster than using casting. In practice,
                casting proved to be several times faster than
                using a generic. However, you probably won't notice performance difference
                in your application.
                (My Test over 100 000 iterations took only a few seconds). So you should
                still use generics because they are type-safe."
                With generics you do not have to cast. So instead of taking into doubt
                the "performanc e" of generics the above statement is saying that
                casting is a very low cost operation :)

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: about generics

                  Tony Johansson <johansson.ande rsson@telia.com wrote:
                  Hello!
                  >
                  No
                  Okay, well I'm happy to write the test instead:

                  using System;
                  using System.Collecti ons;
                  using System.Collecti ons.Generic;
                  using System.Diagnost ics;

                  class Test
                  {
                  const int Iterations = 100;
                  const int Size = 10000000;

                  static void Main()
                  {
                  // Comment out the one you don't want to use
                  // ArrayList list = new ArrayList(Size) ;
                  List<bytelist = new List<byte>(Size );

                  Stopwatch sw = Stopwatch.Start New();
                  for (int i=0; i < Size; i++)
                  {
                  list.Add((byte) i);
                  }
                  int total = 0;
                  for (int i=0; i < Iterations; i++)
                  {
                  foreach (byte b in list)
                  {
                  total += b;
                  }
                  }
                  sw.Stop();
                  Console.WriteLi ne("Total = {0}", total);
                  Console.WriteLi ne("Time = {0}ms", sw.ElapsedMilli seconds);
                  }
                  }

                  Results:

                  For List<T>: around 8 seconds
                  For ArrayList: around 22 seconds

                  Oh, and originally I had Size ten times larger, but that caused
                  swapping with ArrayList - but not with List<T>.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Web site: http://www.pobox.com/~skeet
                  Blog: http://www.msmvps.com/jon.skeet
                  C# in Depth: http://csharpindepth.com

                  Comment

                  Working...