运算符重载与函数调用的性能 [英] Performance of operator overloading vs function call

查看:196
本文介绍了运算符重载与函数调用的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我创建我自己的向量类如下:

Let us suppose I create my own vector class as follows:

<template class T>
class Vector {
private:
  void** ptr;
  // More functions implementing the custom vector
public:
  T& operator[](int iIndex) const {
    return *(T*)ptr[iIndex];
  }
  T& Item(int iIndex) const {
    return *(T*)ptr[iIndex];
  }
}

假设我有一个 Vector< someClass> v 。严格来说,性能方面,访问向量元素的速度更快。

Let say, I have a Vector<someClass> v. Strictly, performance-wise which of these is faster for accessing an element of the vector.


  1. v.Item i)

  2. v [i]

  1. v.Item(i)
  2. v[i]


推荐答案

v [i] 只是 v的语法糖。操作符[](i)

性能上没有差异,在所有其他方面相同的名称。

There will be no difference in performance, insofar that there is no difference in performance between two functions with different names that are identical in all other respects.

这篇关于运算符重载与函数调用的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆