Fastest way to generate a random boolean

The first option – rand.Next(2) executes behind the scenes the following code: if (maxValue < 0) { throw new ArgumentOutOfRangeException(“maxValue”, Environment.GetResourceString(“ArgumentOutOfRange_MustBePositive”, new object[] { “maxValue” })); } return (int) (this.Sample() * maxValue); and for the second option – rand.NextDouble(): return this.Sample(); Since the first option contains maxValue validation, multiplication and casting, the second option is … Read more

The HashSet.removeAll method is surprisingly slow

The behaviour is (somewhat) documented in the javadoc: This implementation determines which is the smaller of this set and the specified collection, by invoking the size method on each. If this set has fewer elements, then the implementation iterates over this set, checking each element returned by the iterator in turn to see if it … Read more

How does _gaq.push([‘_trackPageLoadTime’]) work?

Edit: As of November 16th 2011, the _trackPageLoadTime function has been deprecated and its functionality has been set as a default setting. (Functionally speaking, it has gone from being an opt-in feature to being an opt-out feature.) _setSiteSpeedSampleRate is the new function for setting the sample rate on this feature; its default value is 1 … Read more

How do you test running time of VBA code?

Unless your functions are very slow, you’re going to need a very high-resolution timer. The most accurate one I know is QueryPerformanceCounter. Google it for more info. Try pushing the following into a class, call it CTimer say, then you can make an instance somewhere global and just call .StartCounter and .TimeElapsed Option Explicit Private … Read more

Javascript when to use prototypes

Prototypes are an optimisation. A great example of using them well is the jQuery library. Every time you obtain a jQuery object by using $(‘.someClass’), that object has dozens of “methods”. The library could achieve that by returning an object: return { show: function() { … }, hide: function() { … }, css: function() { … Read more