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

Submission: .Net Core 3 #9

Open
Herocod3r opened this issue Sep 19, 2019 · 0 comments
Open

Submission: .Net Core 3 #9

Herocod3r opened this issue Sep 19, 2019 · 0 comments

Comments

@Herocod3r
Copy link

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace NetCoreSumFiles
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<FileSummerBenchmarker>();
        }
    }

    [MemoryDiagnoser]
    public class FileSummerBenchmarker
    {
        private readonly FileSummer fileSummer = new FileSummer();
        [Benchmark]
        public async Task FileSum()
        {
            await fileSummer.StartProcess();
        }
    }



    public class FileSummer
    {
        private long TotalSum;
        private static readonly string RootDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        public async Task StartProcess()
        {
            
            var tasks = new Task[1000];
            for (int i = 1; i < 1000; i+=10)
            {
                var currentRoot = $"{i.ToString().PadLeft(6,'0')}-{(i+9).ToString().PadLeft(6,'0')}";
                for (int j = i; j <= i+9; j++)
                {
                    tasks[j-1] = GenerateTask(Path.Combine(RootDir,"files",currentRoot,$"{j.ToString().PadLeft(6,'0')}.csv"));
                }
            }

            await Task.WhenAll(tasks);
        }

        private Task GenerateTask(string filename)=>Task.Run(() => ComputeFile(filename));
            
        

        private void ComputeFile(string fileName)
        {
            long totalSum = 0;
            using (var stream = new StreamReader(File.OpenRead(fileName)))
            {
                var numbers = ReadNumbers(stream);
                foreach (var number in numbers)
                {
                    totalSum += number;
                }
            }
            Interlocked.Add(ref TotalSum, totalSum);
        }
        public IEnumerable<int> ReadNumbers (TextReader reader)
        {
            var lastVal = -1;
            while (reader.Peek() >= 0)
            {
                char c = (char)reader.Read ();

                if (!char.IsNumber(c))
                {
                    yield return lastVal;
                    lastVal = -1;
                    continue;
                }

                if (lastVal < 0) lastVal = (int) char.GetNumericValue(c);
                else lastVal = Concatenate(lastVal, (int) char.GetNumericValue(c));
            }
        }
        
        
        private static int Concatenate(int x,int y)
        {
            var pow = 10;
            while(y>=pow) pow*=10;
            return x*pow+y;
        }
    }
}
Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
FileSum 651.6 ms 12.91 ms 17.67 ms 1000.0000 - - 432.92 KB
@mykeels mykeels changed the title .Net Core 3 Submission: .Net Core 3 Sep 19, 2019
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

No branches or pull requests

1 participant