예제 #1
0
        /// <summary>
        ///   Perform one trial, measuring the effectiveness and speed
        ///   of compression.
        /// </summary>
        ///
        /// <param name='label'>the label for the trial</param>
        ///
        /// <param name='compressor'>a function that accepts a string,
        /// and returns a byte array representing the compressed
        /// form</param>
        ///
        /// <param name='decompressor'>a function that accepts a byte
        /// array, and decompresses it, returning a string. </param>
        ///
        /// <param name='s'>the string to compress and decompress</param>
        ///
        /// <param name='nCycles'>the number of cycles to time</param>
        ///
        /// <returns>the CompressionTrialResult describing the trial results</returns>
        ///
        /// <remarks>
        /// </remarks>
        public CompressionTrialResult DoTrial(string label,
                                              Func <string, byte[]> compressor,
                                              Func <byte[], string> decompressor,
                                              string s,
                                              int nCycles)
        {
            byte[] compressed = compressor(s);

            // verify that the compression decompresses correctly
            string uncompressed = decompressor(compressed);

            if (s.Length != uncompressed.Length)
            {
                throw new Exception("decompression failed.");
            }

            // compress the same thing 1000 times, and measure the time
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < nCycles; i++)
            {
                compressed = compressor(s);
            }

            stopwatch.Stop();

            var result = new CompressionTrialResult
            {
                Label             = label,
                CompressedData    = compressed,
                Cycles            = nCycles,
                TimeForManyCycles = stopwatch.Elapsed
            };

            return(result);
        }
예제 #2
0
        /// <summary>
        ///   Perform one trial, measuring the effectiveness and speed
        ///   of compression.
        /// </summary>
        ///
        /// <param name='label'>the label for the trial</param>
        ///
        /// <param name='compressor'>a function that accepts a string,
        /// and returns a byte array representing the compressed
        /// form</param>
        ///
        /// <param name='decompressor'>a function that accepts a byte
        /// array, and decompresses it, returning a string. </param>
        ///
        /// <param name='s'>the string to compress and decompress</param>
        ///
        /// <param name='nCycles'>the number of cycles to time</param>
        ///
        /// <returns>the CompressionTrialResult describing the trial results</returns>
        ///
        /// <remarks>
        /// </remarks>
        public CompressionTrialResult DoTrial(string label,
                                              Func<string, byte[]> compressor,
                                              Func<byte[],string> decompressor,
                                              string s,
                                              int nCycles)
        {
            byte[] compressed = compressor(s);

            // verify that the compression decompresses correctly
            string uncompressed = decompressor(compressed);
            if (s.Length != uncompressed.Length)
                throw new Exception("decompression failed.");

            // compress the same thing 1000 times, and measure the time
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i=0; i < nCycles; i++)
                compressed = compressor(s);

            stopwatch.Stop();

            var result = new CompressionTrialResult
            {
                Label = label,
                CompressedData = compressed,
                Cycles = nCycles,
                TimeForManyCycles = stopwatch.Elapsed
            };
            return result;
        }