Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using strings for Concat #828

Merged
merged 1 commit into from
May 17, 2021
Merged

Using strings for Concat #828

merged 1 commit into from
May 17, 2021

Conversation

SergerGood
Copy link
Collaborator

изображение

To avoid allocations need to use the method
public static string Concat (string? str0, string? str1, ...
instead of
public static string Concat (object? arg0, object? arg1, ...

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
Intel Core i5-9300H CPU 2.40GHz, 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=6.0.100-preview.3.21202.5
  [Host]                  : .NET Core 3.1.15 (CoreCLR 4.700.21.21202, CoreFX 4.700.21.21402), X64 RyuJIT
  MediumRun-.NET Core 3.1 : .NET Core 3.1.15 (CoreCLR 4.700.21.21202, CoreFX 4.700.21.21402), X64 RyuJIT

Job=MediumRun-.NET Core 3.1  Runtime=.NET Core 3.1  IterationCount=15  
LaunchCount=2  WarmupCount=10  
Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
ConcatOfStrings 39.78 ns 3.453 ns 4.841 ns 0.0076 - - 32 B
ConcatOfObjects 53.62 ns 4.601 ns 6.450 ns 0.0134 - - 56 B
    [MemoryDiagnoser]
    [MediumRunJob(RuntimeMoniker.NetCoreApp31)]
    public class Program
    {
        private string _firstStringParam = "1";
        private string _secondStringParam = "2";
        private int _thirdIntParam = 3;
        
        [Benchmark]
        public string ConcatOfStrings()
        {
            return string.Concat(_firstStringParam, _secondStringParam, _thirdIntParam.ToString());
        }
    
        [Benchmark]
        public string ConcatOfObjects()
        {
            return string.Concat(_firstStringParam, _secondStringParam, _thirdIntParam);
        }
    
        private static void Main() => BenchmarkRunner.Run<Program>();
    }

@mikependon mikependon merged commit e3de107 into master May 17, 2021
@SergerGood SergerGood deleted the string-concat branch May 17, 2021 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants