migration C#, Java, C++ (day 10), chrono, time, exceptions
I have mentioned speed quite often. Let’s measure it now!
It is remarkable that C# is dealing with exceptions faster than C++. The code execution is amazing. Anyway, when it comes to thrown exceptions (rarely happens in practice), then C++ is more than 20x faster. I knew how to build in the exceptions to see a difference. I was astonished that C++ was beyond belief compared to C# thrown exceptions. For further study follow these links:
Exceptions part 1
Exceptions part 2
Exceptions part 3
Time, Benchmark and Co.
using System; using System.Diagnostics; namespace DemoApp { public class Benchmark { void Benchmark1() { for (int i = 0; i < 1000; i++) { Console.Write("*"); } Console.WriteLine(); } // void Benchmark2() { for (int i = 0; i < 1000; i++) { try { Console.Write("*"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.WriteLine(); } // void Benchmark3() { for (int i = 0; i < 1000; i++) { try { Console.Write("*"); throw new Exception("OMG"); } catch (Exception ex) { } } Console.WriteLine(); } // void Benchmark4b() { Console.Write("*"); throw new Exception("OMG"); } void Benchmark4a() { for (int i = 0; i < 1000; i++) { try { Benchmark4b(); } catch (Exception ex) { } } Console.WriteLine(); } // public class StopWatch { Stopwatch _Stopwatch = new Stopwatch(); public void start() { _Stopwatch.Start(); } public void stop() { _Stopwatch.Stop(); long lElapsedTicks = _Stopwatch.ElapsedTicks; long lTicksPerSecond = Stopwatch.Frequency; double lMilliseconds = 1000.0 * (double)lElapsedTicks / (double)lTicksPerSecond; Console.WriteLine(lMilliseconds + " ms "); _Stopwatch.Start(); } }; public void test() { DateTime lNow1 = DateTime.Now; // current time DateTime lNow2 = DateTime.Now.AddSeconds(2.0); // current time plus 2 seconds double lSince1970 = DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; // Seconds since 1 Jan 1970 Console.WriteLine("Current local time and date: " + DateTime.Now.ToString("dd MMM yyyy, HH:mm:ss")); Console.WriteLine("1/" + Stopwatch.Frequency); // eg. 1/1,000,000,000 == 1ns // Benchmark DateTime lStart = DateTime.Now; Benchmark1(); DateTime lEnd = DateTime.Now; TimeSpan lDuration = lEnd - lStart; Console.WriteLine("Time passed ms: " + lDuration.TotalMilliseconds); // Benchmarks: let's get more precise now StopWatch lStopwatch = new StopWatch(); lStopwatch.start(); Console.Write("Benchmark1:"); Benchmark1(); lStopwatch.stop(); Console.Write("Benchmark2:"); Benchmark2(); lStopwatch.stop(); Console.Write("Benchmark3:"); Benchmark3(); lStopwatch.stop(); Console.Write("Benchmark4:"); Benchmark4a(); lStopwatch.stop(); Console.ReadKey(); } // } // class } // namespace
example output:
Current local time and date: 12 Mar 2014, 21:35:46
1/3122812
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
****************************************
Time passed ms: 62.0035
Benchmark1:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
63.3714101265142 ms
Benchmark2:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
133.177405492229 ms
Benchmark3:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
6450.41872517462 ms
Benchmark4:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
13223.9343899024 ms
#include <iostream> #include <chrono> #include <time.h> #include <string> using namespace std; using namespace std::chrono; void Benchmark1() { for (int i = 0; i < 1000; i++) { cout << "*"; } cout << endl; } // void Benchmark2() { for (int i = 0; i < 1000; i++) { try { cout << "*"; } catch (exception &ex) { cout << ex.what() << endl; } } cout << endl; } // void Benchmark3() { for (int i = 0; i < 1000; i++) { try { cout << "*"; throw exception("OMG"); } catch (exception &ex) {} } cout << endl; } // void Benchmark4b(){ cout << "*"; throw exception("OMG"); } void Benchmark4a() { for (int i = 0; i < 1000; i++) { try { Benchmark4b(); } catch (exception &ex) {} } cout << endl; } // class Stopwatch { high_resolution_clock _Clock; high_resolution_clock::time_point _From; public: void start() { _From = _Clock.now(); } void stop() { high_resolution_clock::time_point lNow = _Clock.now(); high_resolution_clock::duration lDuration = lNow - _From; intmax_t lNum = high_resolution_clock::period::num; intmax_t lDen = high_resolution_clock::period::den; double lMilliseconds = 1000.0 * lDuration.count() * (double)lNum / (double)lDen; cout << lMilliseconds << " ms " << endl; _From = _Clock.now(); } }; int main() { system_clock c1; // system clock changes will have an impact steady_clock c2; // independent from system clock changes high_resolution_clock c3; system_clock::time_point lTimePoint = system_clock::now(); cout << lTimePoint.time_since_epoch().count() << endl; lTimePoint = lTimePoint + seconds(2); cout << lTimePoint.time_since_epoch().count() << endl; time_t lRawTime; struct tm lLocalTime; const int lTimeStringLength = 50; char lTimeString[lTimeStringLength]; bool b1 = (time(&lRawTime) != -1); // Seconds since 1 Jan 1970 bool b2 = (localtime_s(&lLocalTime, &lRawTime) == 0); // Convert to local time bool b3 = (asctime_s(lTimeString, lTimeStringLength, &lLocalTime) == 0); // convert to string if (b1 && b2 && b3) cout << "Current local time and date: " << lTimeString << endl; else cerr << "Error, cannot get current time." << endl; ratio<1, 10> r1; // 1/10 ratio<2, 10> r2; // 2/10 cout << r1.num << "/" << r1.den << endl; // 1/10 cout << r2.num << "/" << r2.den << endl; // 1/5 cout << system_clock::period::num << "/" << system_clock::period::den << endl; // eg. 1/1,000,000,000 == 1ns microseconds micros(1234); // 1,234 microseconds cout << micros.count() << endl; // 1,234 nanoseconds ns = micros; // 1,234,000 nanoseconds, no loss cout << ns.count() << endl; // 1,234,000 milliseconds ms = duration_cast<milliseconds>(micros); // 1 millisecond, precision loss => duration_cast required micros += ms; cout << micros.count() << endl; // 2,234 // Benchmark steady_clock::time_point lStart = steady_clock::now(); Benchmark1(); steady_clock::time_point lEnd = steady_clock::now(); steady_clock::duration lDuration = lEnd - lStart; if (lDuration == steady_clock::duration::zero()) cout << "no time has passed" << endl; cout << duration_cast<milliseconds>(lDuration).count() << endl; // Benchmarks: let's get more precise now Stopwatch lStopwatch; lStopwatch.start(); cout << "Benchmark1:"; Benchmark1(); lStopwatch.stop(); cout << "Benchmark2:"; Benchmark2(); lStopwatch.stop(); cout << "Benchmark3:"; Benchmark3(); lStopwatch.stop(); cout << "Benchmark4:"; Benchmark4a(); lStopwatch.stop(); cin.get(); return 0; } //
example output:
13946584250282411
13946584270282411
Current local time and date: Wed Mar 12 21:07:05 20141/10
1/5
1/10000000
1234
1234000
2234
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
****************************************
80
Benchmark1:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
86.0049 ms
Benchmark2:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
84.0048 ms
Benchmark3:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
311.018 ms
Benchmark4:*********************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
***************************************************
306.017 ms
package DemoApp; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; public class Benchmark { private void Benchmark1() { for (int i = 0; i < 1000; i++) System.out.print("*"); System.out.println(); } // private void Benchmark2() { for (int i = 0; i < 1000; i++) { try { System.out.print("*"); } catch (RuntimeException ex) { System.out.println(ex.getMessage()); } } System.out.println(); } // private void Benchmark3() { for (int i = 0; i < 1000; i++) { try { System.out.print("*"); throw new RuntimeException("OMG"); } catch (RuntimeException ex) { } } System.out.println(); } // private void Benchmark4b() { System.out.print("*"); throw new RuntimeException("OMG"); } // private void Benchmark4a() { for (int i = 0; i < 1000; i++) { try { Benchmark4b(); } catch (RuntimeException ex) { } } System.out.println(); } // public static class StopWatch { private long _Start = System.nanoTime(); private long _Stop = System.nanoTime(); public final void start() { _Start = System.nanoTime(); } public final void stop() { _Stop = System.nanoTime(); long lElapsedTicks = _Stop - _Start; double lMilliseconds = lElapsedTicks / 1000000.0; System.out.println(lMilliseconds + " ms "); _Start = System.nanoTime(); } } // class public final void test() { Calendar lNow1 = Calendar.getInstance(); // current time Calendar lNow2 = Calendar.getInstance(); lNow2.add(Calendar.SECOND, 2); // current time plus 2 seconds double lSince1970 = System.currentTimeMillis() / 1000.0; // Seconds since 1 Jan 1970 SimpleDateFormat lFormat = new SimpleDateFormat("dd MMM yyyy, HH:mm:ss"); System.out.print("Current local time and date: "); System.out.println(lFormat.format(Calendar.getInstance().getTime())); // Benchmark Calendar lStart = Calendar.getInstance(); Benchmark1(); Calendar lEnd = Calendar.getInstance(); long lDuration = lEnd.getTimeInMillis() - lStart.getTimeInMillis(); System.out.println("Time passed ms: " + lDuration); // Benchmarks: let's get more precise now StopWatch lStopwatch = new StopWatch(); lStopwatch.start(); System.out.print("Benchmark1:"); Benchmark1(); lStopwatch.stop(); System.out.print("Benchmark2:"); Benchmark2(); lStopwatch.stop(); System.out.print("Benchmark3:"); Benchmark3(); lStopwatch.stop(); System.out.print("Benchmark4:"); Benchmark4a();lStopwatch.stop(); try { System.in.read(); } catch (IOException ex) { Logger.getLogger(Benchmark.class.getName()).log(Level.SEVERE, null, ex); } } // public static void main(String[] args) { Benchmark lBenchmark = new Benchmark(); lBenchmark.test(); } // } // class
example output:
Current local time and date: 09 May 2014, 18:03:20
****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
Time passed ms: 16
Benchmark1:****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
18.10766 ms
Benchmark2:****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
14.703688 ms
Benchmark3:****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
25.99924 ms
Benchmark4:****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
49.451426 ms
Posted on March 13, 2014, in C#, C++, Java and tagged benchmark, C#, C# to C++, C++ to Java, chrono, conversion, direct code comparison, Java, Java to C++, migration, milliseconds, nanoTime, time. Bookmark the permalink. Leave a comment.
Leave a comment
Comments 0