There is a current date: DateTime.Now;

In terms of performance, it is more profitable to use:

 1. DateTime.Now.ToString(); 2. DateTime.Now.Year + "." + DateTime.Now.Month + "." ... и т.д. ? 

I was interested in this question because C # does a bunch of things in the ToString method, for example, it leads to the format by default (and other different things from the Globalization assembly). This I learned from the ANTS Memory Profiler .

  • eleven
    1) Why do you care about this question? Do you have reason to believe that this code may become a bottleneck? 2) Have you tried to measure performance yourself? - Dmitry Shevchenko
  • If you have a profiler, then the easiest way to find out what is more productive is to use it - Grundy
  • Each performance question must be accompanied by an explanation of what the bottleneck is. Extreme performance is achieved by rewriting the program on an assembler, but even the authors of the operating system kernel (and not only Windows) refuse from this “optimization”. - VladD
  • 3
    @VladD, well, about the "extreme performance of applications written in assembly language" - when compared with C / C ++ - it is very, very controversial. True, this is only for tiny pieces of assembly language code written not at all at once, but through long and painful enumeration experiments. With a large amount of code, the compiler is much easier to analyze and produce the most productive executable code than even a very experienced assembler who remembers many nuances. Writing a slow assembly code is pretty easy :) - Sergey Rufanov
  • Well, writing code in assembler does not mean abandoning the use of the optimizer with subsequent manual refinement. In any case, any code can be compiled into an assembler, which means that the same performance can be obtained from an assembler. Or better. The question of what price, we leave out of consideration. :) - VladD pm

2 answers 2

Since you are interested in the performance of DateTime (for example, you often write timestamps to the log), you first need to use DateTime.UtcNow - by itself it is much faster .

As for ToString() versus concatenation: in this case concatenation turns out to be a little faster, so in any case you should use it, and better string.Format() (thanks to @devEugene for the reminder).

The question is, who will “overwhelm” someone :).

In general, such things are easily checked independently: a console application, a million iterations, Stopwatch and Release mode to help. For more accurate benchmarking, it is better to use more appropriate means .

I got this (1 000 000 iterations):

DateTime.Now.ToString () - 0.9259747

DateTime.UtcNow.ToString () - 0.8422381

DateTime.Now + string.Format - 0.6951314

DateTime.UtcNow + string.Format - 0.6167482

  • I will only add that the performance comparison may be different in different versions of the compiler, with different system bits, etc. - VladD
  • I do not quite agree with the statement: "in this case, concatenation turns out to be slightly faster." Watching how to write concatenation, if you constantly refer to the .Now property, it is not faster, and if you do DateTime dt = DateTime.Now; and then dt.Year + "", etc., then yes, but also when using + = it is worth remembering that the lines are immutable! And in general, forget about concatenation, there is interpolation of strings (C # 6) or string.Format (). - devEugene
  • Also remind you that when using DateTime.Now.ToString () you can set the format, and if you call just without the format, then the string will return with the current date format (MM / dd / yyyy, dd.MM.yyyy, etc.) as Globalization was seen in the question, and with concatenation then there will be problems, either in night builds, or when unloading on the prod invairment, etc. So no need to crutch ... - devEugene
  • The example is very simple, you make a site with unloading some information and filtering it by date, in the format (dd.MM.yyyy), it goes back to the server and parses, like everything is OK, we return to the customer over the hill, when parsing he writes you that nothing works! - devEugene
  • And nothing that ToString and concatenation formats are different? - Qwertiy

Without tests, offhand such code should be the fastest

 // указать размер достаточный для строки, //но не меньше чем конечная строка. var sb = new StringBuilder(4+1+2+1+2); var date = DateTime.UtcNow; sb.Append(date.Year); sb.Append('.'); sb.Append(date.Month); sb.Append('.'); sb.Append(date.Day); return sb.ToString(); 

StringBuilder will create one buffer which will then return as a string. No additional allocations (as in the case of concatenation), no additional parsing (as in the case of Format), no cultures and other overheads (as in the case of ToString).