示例#1
0
        public static async Task <MatrixInitializeModel> InitMatrixAsync(string path)
        {
            MatrixInitializeModel init = new MatrixInitializeModel();

            HttpResponseMessage response = await ApiClient.client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                init = await response.Content.ReadAsAsync <MatrixInitializeModel>();
            }
            return(init);
        }
示例#2
0
        public static async Task <int[, ]> GetMatrixData(string matrixName)
        {
            int[,] output = new int[Matrix.RowSize, Matrix.ColSize];
            ApiClient.initializeApiClient();
            try {
                MatrixInitializeModel init = await MatricesApi.InitMatrixAsync("api/numbers/init/1000");

                var tasks = new List <Task <MatrixRowModel> >();

                var timer = new Stopwatch();
                timer.Start();

                var batchSize       = 100;
                int numberOfBatches = (int)Math.Ceiling((double)Matrix.RowSize / batchSize);

                for (int i = 0; i < numberOfBatches; i++)
                {
                    int start = i * batchSize;
                    var value = Enumerable.Range(start, batchSize).Select(k => MatricesApi.GetMatrixAsync($"api/numbers/{matrixName}/row/{k}", k));
                    tasks.AddRange(value);
                }

                var result = (await Task.WhenAll(tasks)).Select(m => m);

                Parallel.For(0, result.Count(), y => {
                    int[] values = result.Where(x => x.RowIndex == y).Select(x => x.Value).First();
                    Buffer.BlockCopy(values, 0, output, Matrix.ColSize * y, values.Length);
                });

                timer.Stop();
                Console.WriteLine($"fetched {matrixName} in: {timer.Elapsed.Seconds} Seconds");
                timer.Reset();
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
            finally {
                ApiClient.client.Dispose();
            }

            return(output);
        }