我正在C++项目中使用用C编写的a high performance/parallel graph library。它提供了一个结构stinger
(图形数据结构)和类似的操作
int stinger_insert_edge_pair (struct stinger *G,
int64_t type, int64_t from, int64_t to,
double weight, int64_t timestamp) { .... }
但是,在大多数情况下,我不想指定时间戳,权重或类型。默认参数会很好。同样,类似OOP的接口(interface)也不错:
G->insertEdge(u, v)
而不是insert_edge_pair(G, u, v, ...)
。所以我在考虑创建一个适配器类,如下所示
class Graph {
protected:
stinger* stingerG;
public:
/** default parameters ***/
double defaultEdgeWeight = 1.0;
/** methods **/
Graph(stinger* stingerG);
virtual void insertEdge(node u, node v, double weight=defaultEdgeWeight);
};
insertEdge(...)
方法仅使用适当的参数调用stinger_insert_edge_pair(this->stingerG, ...)
。但是,性能在这里至关重要。与使用此类适配器类相关的性能损失是多少?与使用“裸”库相比,我应该期望性能降低吗?
最佳答案
如果您的insertEgde只是将调用转发到stinger_insert_edge_pair,则对stinger_insert_edge_pair的普通调用与g-> insertEdge的普通调用之间生成的代码没有任何区别(前提是您删除了虚拟说明符)。
比较通过普通调用和适配器调用生成的汇编代码,将为适配器带来的开销提供合理的输入。
insertEdge是否必须是虚拟的?您是否打算拥有Graph的子类?但是,与函数执行本身的实际成本相比,虚拟函数调用的成本几乎可以忽略不计。