Different ways of adding to a dictionary in C#

The performance is almost a 100% identical. You can check this out by opening the class in Reflector.net This is the This indexer: public TValue this[TKey key] { get { int index = this.FindEntry(key); if (index >= 0) { return this.entries[index].value; } ThrowHelper.ThrowKeyNotFoundException(); return default(TValue); } set { this.Insert(key, value, false); } } And this … Read more

JavaScript – run once without booleans

An alternative way that overwrites a function when executed so it will be executed only once. function useThisFunctionOnce(){ // overwrite this function, so it will be executed only once useThisFunctionOnce = Function(“”); // real code below alert(“Hi!”); } // displays “Hi!” useThisFunctionOnce(); // does nothing useThisFunctionOnce(); ‘Useful’ example: var preferences = {}; function read_preferences(){ // … Read more

Counting palindromic substrings in O(n)

The following site shows an algorithm for computing the longest palindromic substring in O(n) time, and does so by computing the longest palindromic substring at every possible center and then taking the maximum. So, you should be able to easily modify it for your purposes. http://www.akalin.cx/2007/11/28/finding-the-longest-palindromic-substring-in-linear-time/ EDIT: The first link looks a little shaky upon … Read more

Fast multiplication/division by 2 for floats and doubles (C/C++)

This is one of those highly-application specific things. It may help in some cases and not in others. (In the vast majority of cases, a straight-forward multiplication is still best.) The “intuitive” way of doing this is just to extract the bits into a 64-bit integer and add the shift value directly into the exponent. … Read more

Using SSE instructions

SSE instructions are processor specific. You can look up which processor supports which SSE version on wikipedia. If SSE code will be faster or not depends on many factors: The first is of course whether the problem is memory-bound or CPU-bound. If the memory bus is the bottleneck SSE will not help much. Try simplifying … Read more

Pandas: slow date conversion

Note: As @ritchie46’s answer states, this solution may be redundant since pandas version 0.25 per the new argument cache_dates that defaults to True Try using this function for parsing dates: def lookup(date_pd_series, format=None): “”” This is an extremely fast approach to datetime parsing. For large data, the same dates are often repeated. Rather than re-parse … Read more

Is the condition of a loop re-evaluated each iteration? [duplicate]

Yes, semantically it will be evaluated on every loop. In some cases, compilers may be able to remove the condition from the loop automatically – but not always. In particular: void foo(const struct rect *r) { for (int i = 0; i < r->width * r->height; i++) { quux(); } } The compiler will not … Read more

tech