Sunday, October 3, 2010

String performance II

the previous post discussed the String.Format and concat. It seems that concat is the most efficient way to add strings together. Now I have the following two functions:


        public static void A()
        {
            var s = "1" + "," + "2" + "," + "3" + "," + "4";
        }

        public static void B()
        {
            var s = String.Join(",", "1", "2", "3", "4");
        }

A wins: A=16 B=310

But when we have to pass to non-string type, the winner is differnet:


        public static void A()
        {
            var s = 1.ToString() + "," + 2.ToString() + "," + 3.ToString() + "," + 4.ToString();
        }

        public static void B()
        {
            var s = String.Join(",", 1, 2, 3, 4);
        }


A=1664 B=496

No comments: