postgresql COUNT(DISTINCT …) very slow

I have a very simple SQL query: SELECT COUNT(DISTINCT x) FROM table; My table has about 1.5 million rows. This query is running pretty slowly; it takes about 7.5s, compared to SELECT COUNT(x) FROM table; which takes about 435ms. Is there any way to change my query to improve performance? I’ve tried grouping and doing … Read more

Why would introducing useless MOV instructions speed up a tight loop in x86_64 assembly?

Background: While optimizing some Pascal code with embedded assembly language, I noticed an unnecessary MOV instruction, and removed it. To my surprise, removing the un-necessary instruction caused my program to slow down. I found that adding arbitrary, useless MOV instructions increased performance even further. The effect is erratic, and changes based on execution order: the … Read more

How to find the kth largest element in an unsorted array of length n in O(n)?

I believe there’s a way to find the kth largest element in an unsorted array of length n in O(n). Or perhaps it’s “expected” O(n) or something. How can we do this? 33 Answers 33 This is called finding the k-th order statistic. There’s a very simple randomized algorithm (called quickselect) taking O(n) average time, … Read more

Why JSF calls getters multiple times

Let’s say I specify an outputText component like this: <h:outputText value=”#{ManagedBean.someProperty}”/> If I print a log message when the getter for someProperty is called and load the page, it is trivial to notice that the getter is being called more than once per request (twice or three times is what happened in my case): DEBUG … Read more

Is it safe to shallow clone with –depth 1, create commits, and pull updates again?

The –depth 1 option in git clone: Create a shallow clone with a history truncated to the specified number of revisions. A shallow repository has a number of limitations (you cannot clone or fetch from it, nor push from nor into it), but is adequate if you are only interested in the recent history of … Read more

Why is Haskell (GHC) so darn fast?

Haskell (with the GHC compiler) is a lot faster than you’d expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an inefficient C program, since GHC compiles Haskell … Read more