Performance question/ideas (c#)
I have 2 pieces of code.
On their own they do nothing.
Think of this merely as a block of code used to make a performance comparison.
I came up with the 2 simple pieces of code above, in order to test which is faster when iterated many many times.
The difference between those 2 code blocks is that in the 2nd block, a string is created and then initialised a billion times. In the top block, the string is created once and then once inside the for loop, the same string is reused (the value is re-assigned).
Now I ran some tests and the block which re-uses the same string is marginally quicker (on my computer, when iterated a billion times, the time difference is about 10ms, on average).
Question: is it better to code for the marginal speed increase OR is it better to create a new string, a billion times?
I ask this, because I am creating an AI and I am craving every little bit of performance I can gain. I am in the process of altering any piece of code which could possibly give a benefit in the performance.
Your opinions please.
I have 2 pieces of code.
On their own they do nothing.
Think of this merely as a block of code used to make a performance comparison.
Code:
int indexMax = 999999999;
string temp = "";
for (var index = 0; index <= indexMax; index++)
{
temp = "a" + "b" + "b" + "b" + "b" + "b" + "b" + "b" + "b";
//other code which uses temp will go here
}
Code:
int indexMax = 999999999;
for (var index = 0; index <= indexMax; index++)
{
string temp1 = "a" + "b" + "b" + "b" + "b" + "b" + "b" + "b" + "b";
//other code which uses temp will go here
}
I came up with the 2 simple pieces of code above, in order to test which is faster when iterated many many times.
The difference between those 2 code blocks is that in the 2nd block, a string is created and then initialised a billion times. In the top block, the string is created once and then once inside the for loop, the same string is reused (the value is re-assigned).
Now I ran some tests and the block which re-uses the same string is marginally quicker (on my computer, when iterated a billion times, the time difference is about 10ms, on average).
Question: is it better to code for the marginal speed increase OR is it better to create a new string, a billion times?
I ask this, because I am creating an AI and I am craving every little bit of performance I can gain. I am in the process of altering any piece of code which could possibly give a benefit in the performance.
Your opinions please.
Last edited: