Problem Statement
We're given an array of Integers stack of length height. The width tells us that at most the width-lowest bits in each entry of xs are set.
Compute an array profile of length width such that profile[i] == max_i with: max_i is maximal with stack[max_i] having the i-th bit set.
How can I achieve this in a more efficient way than below?
Current solution
Currently I go over the columns and check each bit separately.
The following shows my current implementation in Scala. But feel free to give answers in other languages (Java, C, C++), as I am mainly interested in the algorithmic part (optimized for current CPUs).
Scala code:
def tetrisProfile(stack: Array[Int]): Array[Int] = {
var col = 0
val profile = new Array[Int](width)
while(col < width){
var row = 0
var max = 0
while(row < height){
if(((stack(row) >> col) & 1) == 1)
max = row + 1
row += 1
}
profile(col) = max
col += 1
}
return profile
}
Typical values
- array size
heightis 22 - width
widthis 10
Gist with benchmark code
Current results:
original: 2.070s, 2.044s, 1.973s, 1.973s, 1.973s
maxihatop: 0.492s, 0.483s, 0.490s, 0.490s, 0.489s