최적화 및 분기 예측 자의 관점에서이 두 코드간에 차이가 있습니까?분기가있는 함수 시작
첫째 :
void think_and_do(){
if(expression){
//Set_A of instructions
}
else{
//Set_B of instructions
}
}
int main(){
think_and_do();
}
둘째 : 귀하의 예 만약 내가, think_and_do을 godbolt.org과 main
먼저 관찰에서 테스트를했습니다
void do_A(){
//Set_A of instructions
}
void do_B(){
//Set_B of instructions
}
int main(){
if(expression){
do_A();
}
else{
do_B();
}
}
나는 당신이 동일한 코드를 얻을 것이라고 생각 하겠지만 컴파일러 최적화 설정에 달려있다. 둘 다 컴파일하고 어셈블리를 확인할 수 있습니다. – NathanOliver
이것은 경험적으로 가장 잘 대답하는 질문 인 것 같습니다. 반복 가능한 의사 무작위 데이터에 대해 몇 백만 번 반복하고 [측정] (http://stackoverflow.com/questions/11437523/can-i-measure-branch-prediction-failures-on-a-modern-intel -core-cpu? rq = 1). – dlatikay
하나의 키는 두 기능에 필요한 매개 변수 수일 수 있습니다. 함수'think_and_do()'가 모든 매개 변수를 가질 필요가 있고 함수'do_A()'또는'do_B()'만이 모든 매개 변수를 필요로하는 두 번째 경우 (일반적으로'do_A()'가 객체를 생성하고'do_B() '이 객체를 삭제합니다). –