Darrarski
Darrarski

Reputation: 4032

x*x versus table_of_sqr[x] = no difference?

I'm writing heavy image processing algorithms for iPhone using Xcode4 and official SDK. I need to compute squares a lot of times in my code, always for unsigned char x. So, to make it faster I declared a table:

int table[255] = {0, 1, 4, 9,... };

and instead of

int sqr = x * x;

I'm getting values from that table

int sqr = table[ABS(x)];

Unfortunately, when I'm profiling my app in iPhone simulator, it tells me that this one line (getting value from table) takes more than 50% of app execution time, so it's not a big improvement compared to computing sqr at any time. What I'm doing wrong? It should much faster, but I could be wrong, I'm a beginner in this programming environment.

Upvotes: 1

Views: 81

Answers (3)

hotpaw2
hotpaw2

Reputation: 70703

Xcode can show Asm. Look at the assembly language code generated and you will most likely find the abs and indexed array access requires several more ARM instructions than a simple multiply.

Upvotes: 0

user624141
user624141

Reputation:

It is probably not faster. Most CPUs have a single cycle operation for multiplying integers, where as an array lookup means an operation that needs to do a much slower memory access.

Upvotes: 1

yan
yan

Reputation: 20982

Firstly, you're not computing the square root, you're just computing a square, a significantly less-expensive operation. Secondly, if your compiler isn't being clever with caching or constant propagation, a memory look up is usually more expensive than a squaring operation.

Why did you choose to optimize that line originally? Did you profile and decide the old way of doing it wasn't sufficiently efficient/fast?

Upvotes: 4

Related Questions