Ejemplo n.º 1
0
        /// <summary>
        /// Technique 1 - Load all values to uIntCollection and then process only distinct values
        /// </summary>
        /// <param name="filePath">File path of tested data file</param>
        /// <param name="keyValueCollection">Final collection key value pair of order (int) and letter (char)</param>
        /// <param name="uIntCollection">Collection of uint to load all uint from file</param>
        /// <returns>Process time in total milliseconds</returns>
        internal static double T2(string filePath, IKeyValueCollection keyValueCollection, IUIntCollection uIntCollection)
        {
            Console.Write($"{MethodBase.GetCurrentMethod().Name}\t {keyValueCollection.Name.PadRight(40)}\t {uIntCollection.Name.PadRight(30)}\t");

            var stopwatch = Stopwatch.StartNew();

            Parallel.ForEach(File.ReadAllLines(filePath), (line) =>
            {
                if (UInt32.TryParse(line, out uint uintFromString))
                {
                    uIntCollection.AddToCollection(uintFromString);
                }
            });

            Parallel.ForEach(uIntCollection.Distinct(), (number) =>
            {
                SimulatesProcessOneLine(number.ToString());
                var intBytes = BitConverter.GetBytes(number);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(intBytes);
                }

                var byteResult = intBytes;
                var letter     = Convert.ToChar(byteResult[3]);
                var order      = byteResult[2] + (byteResult[1] << 8) + (byteResult[0] << 16);

                var simulatedOrder = SimulateDuplicitValues(number);

                keyValueCollection.AddToCollection(simulatedOrder, 'A');
            });

            var count = keyValueCollection.SortAndWriteToFile();

            Console.Write($"{count}\t\t");

            return(StopStopWatch(stopwatch));
        }
Ejemplo n.º 2
0
        internal static double RunProcessFile(ProcessFile processFile, string filePath, IKeyValueCollection keyValueCollection, IUIntCollection uIntCollection)
        {
            double processTime = 0;

            try
            {
                var time = processFile?.Invoke(filePath, keyValueCollection, uIntCollection);
                processTime = time == null ? 0 : time.Value;
            }
            catch (OutOfMemoryException)
            {
                Console.WriteLine("Not enough memory. Couldn't perform this test.");
                processTime = 0;
            }
            catch (Exception)
            {
                Console.WriteLine("EXCEPTION. Couldn't perform this test.");
                processTime = 0;
            }

            GC.Collect();
            Thread.Sleep(1000);

            return(processTime);
        }