Hot answers tagged

6

Avoid defining your own constants There's no need to use your own constants for the duration of a millisecond, second, minute and so on; just use the right std::chrono types: static const std::unordered_map<std::string, std::chrono::milliseconds> suffix { {"ms", std::chrono::milliseconds{1}}, {"s", std::chrono::seconds{1}}, ...


6

Looks pretty good; these comments are mainly nitpicking: Error messages should be streamed to std::cerr, not std::cout. Whilst that's correctly done in main(), we're using the wrong stream in parseDuration(). I'm not a fan of both modifying the dur parameter and returning it. I would go for accepting a default by value and returning the parsed/default ...


5

Using the following function to test, 62% of your CPU usage is used by DateTime.Now and 15% by Timer.Change static void Main() { var throttle = new Throttle(); for (int index=0; index < int.MaxValue; index++) { throttle.Handle("index = " + index); } } Total runtime: 690.76 seconds DateTime.Now looks deceptively simple ...


Only top voted, non community-wiki answers of a minimum length are eligible