-1

In order to gain performance on the StringBuilder operations, I tried to apply the object pooling.

But pooling the StringBuilder seems to be slower than the original usage on both net8 and net7.

What might cause the problem, any additional settings needed for benchmark dot net?

[MemoryDiagnoser] public class StringBuilderPoolPerformance { [Benchmark] public void WithoutPool() { for (int i = 0; i < 10000; i++) { var stringBuilder = new StringBuilder(); stringBuilder.Append("Hello World" + i); } } [Benchmark] public void WithPool() { var stringBuilderPool = new DefaultObjectPoolProvider().CreateStringBuilderPool(); for (var i = 0; i < 10000; i++) { var stringBuilder = stringBuilderPool.Get(); stringBuilder.Append("Hello World" + i); stringBuilderPool.Return(stringBuilder); } } } 

Benchmark Results

0

1 Answer 1

1

The Mean column does indeed show that the Pool variant is slower, which is caused by the internal administering and locking that the Pool needs to do.

But the Gen0 column tells us that the Pool variant does less than half the number of Garbage Collects compared to the regular variant. And it allocates less than half the memory.

Adam Sitnik (author of Benchmark.NET) says "If GC decides to collect the memory during the actual workload (between Clock.Start and clock.GetElapsed) then it's included in the final results." I think with ~182 Garbage Collects in total we can safely say that the iterations themselves don't trigger a GC, so then the time needed for GC is not visible in the stats (it's spent inside Benchmark.NET code and not inside your code). This could change if you increase the count from 10000 to say 1000000 or even more.

So what your code answers at the moment is only "is new SomeClass() faster than pooling it, while ignoring GC?", and the answer is very much "yes, it is".

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Peter thanks for the comment. Also tried with the higher input but the result was the same. Like the examples below, the same code with the same input, outputs different results. So i thought is there any additional settings need to be made on the benchmarkdotnet side. infoworld.com/article/3616600/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.