简体   繁体   English

优化小3d矢量结构的性能

[英]Optimize small 3d vector struct for performance

I am fairly new to C++ and have the following, let's call it, issue. 我是C ++的新手,有以下内容,让我们称之为问题。 In my computer simulation, I am working quite a lot with vectors. 在我的计算机模拟中,我使用矢量工作了很多。 I built myself a small struct that represents a vector, and would like to learn how to make the operations (like for example the normalize() function more efficient. Also, is there any benefit in using C++'s valarray ? It seems to have some useful methods pre-implemented. 我自己构建了一个表示向量的小结构,并想学习如何进行操作(例如normalize()函数更有效。另外,使用C ++的valarray有什么好处吗?它似乎有一些用处方法预先实施。

I almost exclusively use the normalize() function and addition/substraction/multiplication of vectors. 我几乎只使用normalize()函数和向量的加/减/乘。 Since all my vectors have only three elements, I am hesitant of including 3rd party libraries in my project. 由于我的所有向量只有三个元素,我对在项目中包含第三方库犹豫不决。

This is my struct: 这是我的结构:

struct vector_t {
    int _i, _j, _k;
    vector_t(int i, int j, int k) {
        _i = i;
        _j = j;
        _k = k;
    }
    vector_t() {}
    inline int getI() { 
        return _i; 
    }
    inline int getJ() { 
        return _j; 
    }
    inline int getK() { 
        return _k; 
    }
    inline void setI(int val) { 
        _i = val; 
    }
    inline void setJ(int val) { 
        _j = val; 
    }
    inline void setK(int val) { 
        _k = val; 
    }
    void normalize() {
        float length = sqrt(_i*_i + _k*_k + _j*_j);
        _i /= length;
        _j /= length;
        _k /= length;
    }
};

And my questions: 我的问题是:

  • How can I speed up the normalize() function or is this already the most efficient way? 如何加速normalize()函数或者这是否是最有效的方法?
  • What would be a more C++-onic way to implement such a struct/class while keeping memory and computer time usages low? 在保持内存和计算机时间用量较低的同时实现这样的结构/类的更多C ++方法是什么?
  • Should I prefer a valarray over my own type? 我是否应该选择超过我自己类型的valarray?

Use a fast reciprocal square root function to calculate 1 / length and then multiply each element by this factor. 使用快速倒数平方根函数计算1 /长度,然后将每个元素乘以此因子。 As well as being a faster function than sqrt this also trades 3 expensive division operations for 3 relatively cheap multiplies: 除了比sqrt更快的功能之外,这还有3个相对便宜的乘法交易3个昂贵的除法运算:

struct vector_t {
    float _i, _j, _k;

    // ...

    void normalize() {
        float r_length = Q_rsqrt(_i*_i + _k*_k + _j*_j);
        _i *= r_length;
        _j *= r_length;
        _k *= r_length;
    }
};

Note: you might want to think about how you're going to handle the pathological case where i == j == k == 0 . 注意:您可能想要考虑如何处理i == j == k == 0的病态情况。

That won't make a big difference but your constructor should look like this : 这不会有很大的不同,但你的构造函数应该是这样的:

vector_t(int i, int j, int k)
: _i(i), _j(j), _k(k)
{
}

For optimization you should look into SSE : http://www.cortstratton.org/articles/OptimizingForSSE.php 为了优化,您应该查看SSE: http//www.cortstratton.org/articles/OptimizingForSSE.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM