[問題] 為什麼CRTP+variant還是比virtual快很多
看板C_and_CPP作者ab4daa (nooooooooooooooooooo)時間5年前 (2020/01/02 04:28)推噓1(1推 0噓 18→)留言19則, 4人參與, 5年前最新討論串1/2 (看更多)
一開始是看到這篇2013的文章
https://eli.thegreenplace.net/2013/12/05/
the-cost-of-dynamic-virtual-calls-vs-static-crtp-dispatch-in-c/
短: https://reurl.cc/M75jGK
裡面只用一個CRTP impl 所以compiler可以直接inline
但是我實際用到virtual是像這樣
集中一堆ABC然後全呼叫一遍:
vector<ABC*> bases;
for(auto& it : bases)
it->virtual_func();
改成CRTP的寫法大概像這樣:
https://godbolt.org/z/LygFKT
我的想像是
visit時不是應該也要先看實際型別是甚麼
再呼叫相對應函數嗎
這跟vtable的行為很像吧
為什麼量測時間 CRTP還是比virtual快了5~7倍?
(i5 7400 + vs2019)
我的程度看組語或是看variant header都很痛苦
所以來洗耳恭聽大大的教訓
感恩
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.183.174 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1577939284.A.9A6.html
→
01/02 21:04,
5年前
, 1F
01/02 21:04, 1F
→
01/02 21:07,
5年前
, 2F
01/02 21:07, 2F
→
01/02 21:07,
5年前
, 3F
01/02 21:07, 3F
→
01/02 23:09,
5年前
, 4F
01/02 23:09, 4F
→
01/02 23:09,
5年前
, 5F
01/02 23:09, 5F
→
01/02 23:09,
5年前
, 6F
01/02 23:09, 6F
推
01/03 04:51,
5年前
, 7F
01/03 04:51, 7F
→
01/03 04:51,
5年前
, 8F
01/03 04:51, 8F
→
01/03 04:52,
5年前
, 9F
01/03 04:52, 9F
→
01/03 04:52,
5年前
, 10F
01/03 04:52, 10F
→
01/03 04:53,
5年前
, 11F
01/03 04:53, 11F
→
01/03 04:53,
5年前
, 12F
01/03 04:53, 12F
→
01/03 04:56,
5年前
, 13F
01/03 04:56, 13F
→
01/03 04:56,
5年前
, 14F
01/03 04:56, 14F
→
01/03 04:59,
5年前
, 15F
01/03 04:59, 15F
→
01/03 05:01,
5年前
, 16F
01/03 05:01, 16F
→
01/07 01:47,
5年前
, 17F
01/07 01:47, 17F
→
01/07 01:47,
5年前
, 18F
01/07 01:47, 18F
→
01/07 01:54,
5年前
, 19F
01/07 01:54, 19F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):