What about performance here

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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
 
Back
Top