As MSalters already stated very toroughly, it is unlikely to help. Furthermore it is almost imposible to predict if it helps. I have created two examples for you where you can see that -- one using a pointer array and a second one if-else-clause.
Example 1
#include <array>
#include <functional>
int test1(int* i)
{
return *i * *i * *i;
}
int test2(int* i)
{
return *i * *i* *i* *i* *i* *i;
}
std::array<int (*)(int*), 2> funcs{test1, test2};
int testing(int v)
{
int testme = v + 4 ;
return funcs[v](&testme);
}
with its assembly
test1(int*): # @test1(int*)
movl (%rdi), %ecx
movl %ecx, %eax
imull %ecx, %eax
imull %ecx, %eax
retq
test2(int*): # @test2(int*)
movl (%rdi), %ecx
movl %ecx, %eax
imull %ecx, %eax
imull %ecx, %eax
imull %eax, %eax
retq
testing(int): # @testing(int)
pushq %rax
leal 4(%rdi), %eax
movl %eax, 4(%rsp)
movslq %edi, %rax
leaq 4(%rsp), %rdi
callq *funcs(,%rax,8)
popq %rcx
retq
funcs:
.quad test1(int*)
.quad test2(int*)
Here is Example2
int test1(int* i)
{
return *i * *i * *i;
}
int test2(int* i)
{
return *i * *i* *i* *i* *i* *i;
}
int testing(int v)
{
int testme = v + 4 ;
return (v == 0) ? test1(&testme) : test2(&testme);
}
with its corresponding assembly
test1(int*): # @test1(int*)
movl (%rdi), %ecx
movl %ecx, %eax
imull %ecx, %eax
imull %ecx, %eax
retq
test2(int*): # @test2(int*)
movl (%rdi), %ecx
movl %ecx, %eax
imull %ecx, %eax
imull %ecx, %eax
imull %eax, %eax
retq
testing(int): # @testing(int)
leal 4(%rdi), %eax
movl %eax, %ecx
imull %eax, %ecx
imull %eax, %ecx
testl %edi, %edi
movl $1, %eax
cmovnel %ecx, %eax
imull %ecx, %eax
retq
Instruction-count wise they are very similar (maybe an expert can help us out here and explain it further). The if-else-statement breaks doun to this one instruction cmovnel
. I doubt it is going to make much difference.
I think you will always have to measure in your exact setup. Compiler optimization is always difficult to predict in example cases.
There will NOT be a true or false answer possible here.