Re: c# interview question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 12 Mar, 10:45, "Mike Schilling" <a...@xxxxxxxxxxxxxxxx> wrote:
"Steve Bugden" <SteveBug...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message

news:606BB626-8DDC-40CD-87F7-7A9B00608284@xxxxxxxxxxxxxxxx





Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLine("The value is: " + i);
Console.WriteLine("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

By definition, they mean exactly the same thing, so it doesn't matter which
one you use. (If it were possible for "i" to be null, the first one would
be better, since it could never cause a null reference exception, but a
value type can't be null.) The analyses in other posts in the thread aren't
about the difference between these two statements, they're about what IL a
particular version of the compiler happens to generate. If these people
expect you to know that off the top of your head, they're idiots. Likewise,
if these people think that the difference between the two sets of generated
IL cause any measurable difference in performance, they're idiots.





I said I would use: Console.WriteLine("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Sorry but that just isn't true.
The IL shows perfectly the different behaviours of the two lines. The
first will box into an object, call Concat(object, object) which
internally will call Concat(string, string). The second only calls
Concat(string, string) Any internal compiler optimisations would have
to honour that behaviour. So the IL disassembly shows exactly what the
difference between the two lines of code are and would not be markedly
different regardless of whether you compiled it under VS or say Mono.
The OP wanted to know what the difference was based on his interview
and he got several good answers.

Regarding performance. One is more performant than the other. Running
the above example won't yield a noticable difference in speed, doing
so with a more complex example in a tight loop over millions of
iterations would. I don't see why you'd use the less performant, less
dotnet (read strongly typed is good) approach and I don't understand
this sort of backlash against trying to make your code run as quickly
and efficiently as possible. The method Go() at the end of this post
shows the second loop runs 5-10pc quicker than the first loop.

As for whether anyone should know that off the top of their head, I'd
expect them to know about boxing and what problems it can cause and
I'd expect them to be able to ask the right questions to get to the
answer even if they needed help to do so.

And good luck on future interviews Steve.

private static void Go()
{
int s1 = System.Environment.TickCount;
string s;
int c;
for(c=0;c<10000000;c++)
{
s = "The value is:" + c;
s = string.Empty;
}
int s2 = System.Environment.TickCount;
for(c=0;c<10000000;c++)
{
s = "The value is:" + c.ToString();
s = string.Empty;
}
int s3 = System.Environment.TickCount;
Console.WriteLine("{0}\n\r{1}\n
\r{2}",s1.ToString(),s2.ToString(),s3.ToString());
Console.ReadLine();
}

.



Relevant Pages