Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Double.ToString() performance issue

Tags:

c#

.net

I have the following method to convert a double array to a List<string>:

    static Dest Test(Source s)
    {
        Dest d = new Dest();

        if (s.A24 != null)
        {
            double[] dd = s.A24;

            int cnt = dd.Length;

            List<string> lst = new List<string>();

            for (int i = 0; i < cnt; i++)
                lst.Add(((double)dd[i]).ToString());

            d.A24 = lst;
        }
        else
        {
            d.A24 = null;
        }

        return d;
    }

Doing a List.Add() in a loop seems like the fastest way according to my benchmarks beating all the various LINQ and Convert tricks.

This is really slow. 2400ms for a million calls (Any CPU, prefer 64-bit). So I was experimenting with various ways to make it faster. Let's assume I cannot cache the source or dest lists, etc obviously.

So anyways, I stumbled across something weird here... if I change the lst.Add() line to cast to a decimal instead of a double, it is much, MUCH faster. 900ms vs 2400ms.

Here's my questions

1) decimal has greater accuracy then double, so I shouldn't lose anything in the type cast, correct?

2) why is the Decimal.ToString() so much faster then Double.ToString()?

3) is this a reasonable optimization, or am I missing some key detail where this will come back to bite me?

I'm not concerned about using up a little bit more memory, I am only concerned about performance.

Nothing sophisticated for the test data at this point, just using:

s.A24 = new double[] { 1.2, 3.4, 5.6 };
like image 983
SledgeHammer Avatar asked May 06 '16 20:05

SledgeHammer


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

For what it's worth, I ran the following and got different results, with decimal usually taking slightly longer (but both calls of the calls to lst.Add() and number.ToString() being roughly equivalent).

What type of collection is A24 in your code? I wouldn't be surprised if the additional overhead you're seeing is actually in casting or something you're not currently looking at.

var iterations = 1000000;

var lst = new List<string>();

var rnd = new Random();
var dblArray = new double[iterations];
for (var i = 0; i < iterations; i++)
    //INTERESTING FINDING FROM THE COMMENTS
    //double.ToString() is faster if this line is rnd.NextDouble()
    //but decimal.ToString() is faster if hard-coding the value "3.5" 
    //(despite the overhead of casting to decimal)
    dblArray[i] = rnd.NextDouble();

var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < iterations; i++)
    lst.Add(dblArray[i].ToString());
sw.Stop();
//takes 280-300 MS
Debug.WriteLine("Double loop MS: " + sw.ElapsedMilliseconds);

//reset list
lst = new List<string>();
sw.Restart();
for (var i = 0; i < iterations; i++)
    lst.Add(((decimal)dblArray[i]).ToString());
sw.Stop();
//takes 280-320 MS
Debug.WriteLine("Decimal loop MS: " + sw.ElapsedMilliseconds);
like image 113
Colin Avatar answered Oct 01 '22 22:10

Colin