溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++如何將計(jì)算從運(yùn)行時(shí)移動(dòng)到編譯時(shí)

發(fā)布時(shí)間:2021-11-25 16:15:21 來(lái)源:億速云 閱讀:111 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“C++如何將計(jì)算從運(yùn)行時(shí)移動(dòng)到編譯時(shí)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Per.11:將計(jì)算從運(yùn)行時(shí)移動(dòng)到編譯時(shí)

Reason(原因)

為了減少代碼大小和執(zhí)行時(shí)間。通過(guò)常量避免數(shù)據(jù)競(jìng)爭(zhēng)。為了在編譯時(shí)捕捉錯(cuò)誤(同時(shí)消除錯(cuò)誤處理代碼)

Example(示例)

double square(double d) { return d*d; }
static double s2 = square(2);    // old-style: dynamic initialization

constexpr double ntimes(double d, int n)   // assume 0 <= n
{
       double m = 1;
       while (n--) m *= d;
       return m;
}
constexpr double s3 {ntimes(2, 3)};  // modern-style: compile-time initialization

向s2初始化這樣的代碼很常見,特別是比square稍微復(fù)雜一點(diǎn)初始化代碼。然而,和s3的初始化相比,存在兩個(gè)問(wèn)題:

  • we suffer the overhead of a function call at run time

  • 我們需要負(fù)擔(dān)執(zhí)行時(shí)的函數(shù)調(diào)用所需的代價(jià)。

  • s2 just might be accessed by another thread before the initialization happens.

  • 在被初始化之前,s2可能被另外的線程訪問(wèn)。

Note: you can't have a data race on a constant.

注意:常量不會(huì)發(fā)生數(shù)據(jù)競(jìng)爭(zhēng)。

Example(示例)

考慮一種提供一個(gè)存儲(chǔ)小對(duì)象于自身,存儲(chǔ)大對(duì)象于堆的句柄。

constexpr int on_stack_max = 20;

template<typename T>
struct Scoped {     // store a T in Scoped
       // ...
   T obj;
};

template<typename T>
struct On_heap {    // store a T on the free store
       // ...
       T* objp;
};

template<typename T>
using Handle = typename std::conditional<(sizeof(T) <= on_stack_max),
                   Scoped<T>,      // first alternative
                   On_heap<T>      // second alternative
              >::type;

void f()
{
   Handle<double> v1;                   // the double goes on the stack
   Handle<std::array<double, 200>> v2;  // the array goes on the free store
   // ...
}

假設(shè)Scoped和On_head提供了兼容的用戶接口。這里我們?cè)诰幾g時(shí)計(jì)算最優(yōu)的類型。類似的技術(shù)可以用于選擇最優(yōu)的函數(shù)調(diào)用。

Note(注意)

理想狀態(tài)時(shí){不要}試圖在運(yùn)行時(shí)執(zhí)行每一件事。顯然由于大多數(shù)計(jì)算依靠輸入信息,所以無(wú)法移動(dòng)到編譯時(shí)計(jì)算,但是復(fù)雜的編譯時(shí)計(jì)算會(huì)嚴(yán)重的增加編譯時(shí)間并使調(diào)試復(fù)雜化。甚至可能由于引入編譯時(shí)計(jì)算使代碼變慢。不可否認(rèn),這種情況非常少見,但是通過(guò)將一個(gè)通常的計(jì)算強(qiáng)制分為獨(dú)立的最優(yōu)化子計(jì)算過(guò)程,有可能使指令緩存效率變低。

Enforcement(實(shí)施建議)

  • Look for simple functions that might be constexpr (but are not).

  • 尋找可以(但是沒(méi)有)定義為constexpr的簡(jiǎn)單函數(shù)。

  • Look for functions called with all constant-expression arguments.

  • 尋找使用常量表達(dá)式參數(shù)調(diào)用的函數(shù)。

  • Look for macros that could be constexpr.

  • 尋找可以定義為constexpr。

“C++如何將計(jì)算從運(yùn)行時(shí)移動(dòng)到編譯時(shí)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI