What about performance here

T

Tony Johansson

Hello!

When I use string.format in this way will that hurt the performance. I mean
you should not concatenate a lot of string together you should instead use
the StringBuilder. So I wonder how will the runtime do in this case.?

foreach (Animal animal in animalManager.Animals.ToArray())
{
lstOutput.Items.Add(string.Format("{0,-20}{1,-20}{2,-20}{3,-20}
{4,-20} {5,-20} {6,-20} {7,-20} {8,-20}", animal.ID, animal.Category,
animal.MyAnimal, animal.Name,
animal.Age.ToString(), animal.Gender, animal.Aggressive,
animal.Housing, foodList));//,"No Extra Info"));
}

//Tony
 
A

Arne Vajhøj

When I use string.format in this way will that hurt the performance. I mean
you should not concatenate a lot of string together you should instead use
the StringBuilder. So I wonder how will the runtime do in this case.?

foreach (Animal animal in animalManager.Animals.ToArray())
{
lstOutput.Items.Add(string.Format("{0,-20}{1,-20}{2,-20}{3,-20}
{4,-20} {5,-20} {6,-20} {7,-20} {8,-20}", animal.ID, animal.Category,
animal.MyAnimal, animal.Name,
animal.Age.ToString(), animal.Gender, animal.Aggressive,
animal.Housing, foodList));//,"No Extra Info"));
}

I would expect by far the most costly operation in that code
to be the parsing and processing of the format string.

But I would not expect any performance problems with
normal data sizes.

If you tried to add 100 million items, then it may take
some time, but you would have run out of memory long before
that and there are really no point in having a really
large number of items displayed in a GUI anyway.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top