I'm working on a problem where I need to return the fifth power of a digit 0-9 and I had thought I could speed up the program by switching
int pow(int a){return a*a*a*a*a;}
out for
int pow(int a){
switch(a){
case 0: return 0; break;
case 1: return 1; break;
case 2: return 32; break;
case 3: return 243; break;
case 4: return 1024; break;
case 5: return 3125; break;
case 6: return 7776; break;
case 7: return 16807; break;
case 8: return 32768; break;
case 9: return 59049; break;}
return 0;}
but I realized that the program runs about 20% faster with the first function than it does with the second, despite the first requiring 5 multiplication operations and the second only invokes a single switch statement, why is this?