相关疑难解决方法(0)

浮点除法与浮点乘法

通过编码是否有任何(非微优化)性能增益

float f1 = 200f / 2
Run Code Online (Sandbox Code Playgroud)

在比较中

float f2 = 200f * 0.5
Run Code Online (Sandbox Code Playgroud)

几年前我的一位教授告诉我,浮点除法比浮点乘法慢,但没有详细说明原因.

这句话适用于现代PC架构吗?

UPDATE1

关于评论,请同时考虑这个案例:

float f1;
float f2 = 2
float f3 = 3;
for( i =0 ; i < 1e8; i++)
{
  f1 = (i * f2 + i / f3) * 0.5; //or divide by 2.0f, respectively
}
Run Code Online (Sandbox Code Playgroud)

更新2 从评论中引用:

[我想]知道什么是算法/架构要求导致>除法在硬件上比复制要复杂得多

c++ floating-point micro-optimization

67
推荐指数
5
解决办法
5万
查看次数

为什么乘法比划分便宜?

我最近写了一个Vector 3类,并将我的normalize()函数提交给了朋友.他说它很好,但是我应该尽可能地乘以倒数,因为在CPU时间里"乘法比划分便宜".

我的问题很简单,那是为什么?

theory performance cpu-usage

8
推荐指数
2
解决办法
3809
查看次数

Identify slow code to optimize build time

I'm using these Compilation Swift Flag to identify codes that slow down the compilation time:

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100
Run Code Online (Sandbox Code Playgroud)

Then after building, I get warnings like these:

Instance method 'startFadePositionTitle()' took 2702ms to type-check (limit: 500ms)

for this part of the code:

    func startFadePositionTitle() -> CGFloat {
        let value: CGFloat = ((backgroundImage.frame.height/2 - contentTitle.frame.height/2) - navbarView.frame.height)/2
        return value
    }
Run Code Online (Sandbox Code Playgroud)

Can someone explains me what is wrong in this method and what could I possibly improve?

xcode ios swift

6
推荐指数
1
解决办法
1025
查看次数

为什么这段代码这么慢?

所以我有这个用来计算统计数据的函数(min/max/std/mean).现在的问题是它通常在10,000乘15,000矩阵上运行.矩阵存储vector<vector<int> >在类中.现在创建和填充所述矩阵变得非常快,但是当它归结为统计部分时,它变得非常缓慢.

例如,为了一次读取地理基准的所有像素值,一个像素大约需要30秒.(它涉及大量复杂的数学运算,以便将像素值正确地映射到相应的点),以计算整个矩阵的统计数据,大约需要6分钟.

void CalculateStats()
{
    //OHGOD
    double new_mean = 0;
    double new_standard_dev = 0;

    int new_min = 256;
    int new_max = 0;

    size_t cnt = 0;
    for(size_t row = 0; row < vals.size(); row++)
    {
        for(size_t col = 0; col < vals.at(row).size(); col++)
        {
            double mean_prev = new_mean;
            T value = get(row, col);
            new_mean += (value - new_mean) / (cnt + 1);
            new_standard_dev += (value - new_mean) * (value - mean_prev);

            // find new max/min's
            new_min = …
Run Code Online (Sandbox Code Playgroud)

c++ performance

5
推荐指数
4
解决办法
1571
查看次数