What are copy elision and return value optimization?

Introduction For a technical overview – skip to this answer. For common cases where copy elision occurs – skip to this answer. Copy elision is an optimization implemented by most compilers to prevent extra (potentially expensive) copies in certain situations. It makes returning by value or pass-by-value feasible in practice (restrictions apply). It’s the only … Read more

Optimum way to compare strings in JavaScript? [duplicate]

You can use the localeCompare() method. string_a.localeCompare(string_b); /* Expected Returns: 0: exact match -1: string_a < string_b 1: string_a > string_b */ Further Reading: MDN: String.prototype.localeCompare Stack Overflow – Is there a JavaScript strcmp()? Tutorials Point: JavaScript String – localeCompare() Method

Flatten an irregular (arbitrarily nested) list of lists

Using generator functions can make your example easier to read and improve performance. Python 2 Using the Iterable ABC added in 2.6: from collections import Iterable def flatten(xs): for x in xs: if isinstance(x, Iterable) and not isinstance(x, basestring): for item in flatten(x): yield item else: yield x Python 3 In Python 3, basestring is … Read more

What is the most effective way for float and double comparison?

Be extremely careful using any of the other suggestions. It all depends on context. I have spent a long time tracing bugs in a system that presumed a==b if |a-b|<epsilon. The underlying problems were: The implicit presumption in an algorithm that if a==b and b==c then a==c. Using the same epsilon for lines measured in … Read more

Performance optimization strategies of last resort [closed]

OK, you’re defining the problem to where it would seem there is not much room for improvement. That is fairly rare, in my experience. I tried to explain this in a Dr. Dobbs article in November 1993, by starting from a conventionally well-designed non-trivial program with no obvious waste and taking it through a series … Read more

How to iterate over a list in chunks

def chunker(seq, size): return (seq[pos:pos + size] for pos in range(0, len(seq), size)) Works with any sequence: text = “I am a very, very helpful text” for group in chunker(text, 7): print(repr(group),) # ‘I am a ‘ ‘very, v’ ‘ery hel’ ‘pful te’ ‘xt’ print(‘|’.join(chunker(text, 10))) # I am a ver|y, very he|lpful text animals … Read more

tech