मैं हॉटस्पॉट कोड है जो एक तंग पाश में चलता है:बचना पुनरावृत्ति/पाश unswitching
for (i = 0; i < big; i++)
{
if (condition1) {
do1();
} else if (condition2) {
do2();
} else {
do3();
}
// Shared code goes here
// More shared code goes here
}
condition1
के बाद से और condition2
अपरिवर्तनीय हैं, मैं
if (condition1) {
for (i = 0; i < big; i++)
{
do1();
// Shared code goes here
// More shared code goes here
}
} else if (condition 2) {
for (i = 0; i < big; i++)
{
do2();
// Shared code goes here
// More shared code goes here
}
} else {
for (i = 0; i < big; i++)
{
do3();
// Shared code goes here
// More shared code goes here
}
}
के पाश unswitched यह काफी बेहतर चलाता है, लेकिन मुझे आश्चर्य है कि खुद को दोहराए बिना ऐसा करने का एक चालाक तरीका है?
शायद आप फ़ंक्शन पॉइंटर का उपयोग कर सकते हैं। शर्तों के आधार पर सेट करें, फिर उस एकल लूप के अंदर उपयोग करें। –
क्या स्थिति 1 और condition2 संकलित समय पर ज्ञात हैं? – Straw1239
@ स्ट्रॉ 1239: नहीं, जब भी फ़ंक्शन कहा जाता है तो वे अलग-अलग हो सकते हैं। – Charles