﻿namespace HighPerformance
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    using BenchmarkDotNet.Attributes;

    [CoreJob, ClrJob]
    public class StringOperation
    {
        [Params(1000)]
        public int N { get; set; }

        [Benchmark(Baseline = true)]
        public void ConcatenationWithPlus()
        {
            var result = "";
            for (int i = 0; i < N; i++)
            {
                result += i.ToString();
            }
        }

        [Benchmark]
        public void ConcatenationWithFormat()
        {
            var result = "";
            for (int i = 0; i < N; i++)
            {
                result = string.Format("{0}{1}", result, i.ToString());
            }
        }

        [Benchmark]
        public void ConcatenationWithJoin()
        {
            var result = string.Join(" ", Enumerable.Range(0, N));
        }

        [Benchmark]
        public void ConcatenationWithConcat()
        {
            var result = string.Concat(Enumerable.Range(0, N));
        }

        [Benchmark]
        public void ConcatenationWithStringBuilder()
        {
            var builder = new StringBuilder();
            for (int i = 0; i < N; i++)
            {
                builder.Append(i.ToString());
            }

            builder.ToString();
        }

        [Benchmark]
        public void ConcatenationWithAggregation()
        {
            var result = Enumerable.Range(0, N).Select(i=>i.ToString()).Aggregate((partial, current) => $"{partial}{current}");
        }
    }
}
