18

It's called "loop fusion". It's often more efficient, in the sense of doing more work per loop iteration and sometimes (as you say) other advantages. On the other hand, the fused loop in your example may also put more pressure on the CPU's cache prefetch system. So do test it before declaring it more efficient.


7

I think your friend somewhat presents a false dichotomy. I will just give one example: when it first came out, the Self VM was one of the fastest dynamic language implementations. In fact, the Smalltalk VM written in Self that shipped as part of the Self system was one of the fastest Smalltalk VMs of its time, despite being written in a dynamic language (...


2

Algorithm in the question Although it is correct, it is rather slow. In the function Omega(k), b goes from 1 to the largest prime divisor of $k$, which is of order $\dfrac {\pi^2n} {12\,\log n}$ on average for $1 \le k \le n$ as mentioned by the On-Line Encyclopedia of Integer Sequences. So, the running time of computing $g(n)$ is $O(\dfrac {n^2}{\log n})$. ...


2

If the language allows that the same variable x could have different types on different executions of a procedure, that doesn't mean the programmer actually wants to use this feature, or does use the feature. Typically you have three cases: 1. x has always the same type (but without proof). 2. x has the same type in 99.99% of all cases. 3. It happens quite ...


1

Minimizing $\|\theta\|$ is equivalent to minimizing $\|\theta\|^2/2$, in the sense that the minimum is achieved for the same value of $\theta$. Since our goal is primarily to find $\theta$, this substitution is not unreasonable. Why was the substitution done? I don't know. Perhaps because it makes the resulting optimization easier; or just because it ...


1

“Branch prediction” will predict whether a particular branch is taken or not. The prediction can be right or wrong; whoever designs the hardware for it will want the prediction to be correct as often as possible. Starting with a predicted branch, we don’t know if the instructions we execute should actually be executed, so we start with speculative execution. ...


1

Here's a hint (and a sketch). I believe the problem is NP-hard and that it is equivalent to Subset Sum. Reducing from Subset Sum: Suppose your instance has sum $S$ and values $v_1, v_2, \dots, v_n$. Create $n+1$ states, one with $S$ electorals that Party $A$ is guaranteed to win. The remaining $n$ states is such that $i$ has $v_i$ electorals. These cost ...


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