G
G
Grand Silence2014-08-12 23:41:48
PHP
Grand Silence, 2014-08-12 23:41:48

How does initializing variables in a C# loop affect performance?

Good evening, C# experts. I asked myself this question: "Does the initialization of variables in the loop affect performance? Or should variables be initialized outside the loop?". Performance is extremely important, as the application is multi-threaded (about 100 threads).
Example 1 (initialization outside the loop):

string response;
            for (int i = 0; i < 4; i++)
            {
                response = Send("DoSomething", "arguments");
            }

Example 2 (initialization inside the loop):
for (int i = 0; i < 4; i++)
            {
                string response = Send("DoSomething", "arguments");
            }

Which option is more productive? and is there any difference in the compiled IL code? (reflector'a is not at hand). Thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Volintsev, 2016-06-08
@copist

Notice: Undefined property: index::$TestUrl in /home/lucifer/php/Core/modules/router.php on line 29

Notice: Undefined property: index::$Test in /home/lucifer/php/Core/modules/router.php on line 29

Errors in the router.php file. On line 29.
Presumably in
It is not clear from the context what exactly is in the variables $id, $url, $func, $f

E
EndUser, 2014-08-13
@mixtape774

"How the initialization of variables in a loop affects performance" is always bad.
On the other hand, you could not be lazy and run both options for 100e6 cycles.
On the third hand, you can read "a rope long enough to shoot yourself in the foot" so that the level of your questions, te-skat, becomes less trivial.

M
mequasar, 2014-09-09
@mequasar

Of course the first option is faster.
You don't waste time creating a string object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question