为什么即使在优化级别为3的情况下,向量分配也要花费这么多时间? [英] Why vector allocation take so much time even with optimization level 3?

查看:84
本文介绍了为什么即使在优化级别为3的情况下,向量分配也要花费这么多时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前我在这里问过类似的问题

Previously I asked this similar question here

Android NDK:vector.resize()太慢,与分配有关?

问题是这段代码

#include <chrono>
#include <android/log.h>
#include <vector>

while (true)
    {
        const int sz = 2048*2048*3;
        std::vector<unsigned char> v;
        {
            auto startTime = std::chrono::system_clock::now();
            v.resize(sz);
            auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - startTime);
            __android_log_print(ANDROID_LOG_ERROR, "READFILE 1", "v.resize(%d) time : %lld\n", sz, duration.count());
        }
        {
            auto startTime = std::chrono::system_clock::now();
            v.resize(0);
            auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - startTime);
            __android_log_print(ANDROID_LOG_ERROR, "READFILE 2", "v.resize(0) time : %lld\n", duration.count());
        }
        {
            auto startTime = std::chrono::system_clock::now();
            v.resize(sz);
            auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - startTime);
            __android_log_print(ANDROID_LOG_ERROR, "READFILE 3", "v.resize(%d) time : %lld\n", sz, duration.count());
        }
    }

带我大约500毫秒(请看上面我喜欢的问题)

Took me aroud 500 milliseconds (look at question that I liked above)

34.4171: v.resize(12582912) time : 845977
34.9682: v.resize(0) time : 550995
35.5293: v.resize(12582912) time : 561165
36.6121: v.resize(12582912) time : 530845
37.1612: v.resize(0) time : 548528
37.7183: v.resize(12582912) time : 556559
38.7811: v.resize(12582912) time : 515162
39.3312: v.resize(0) time : 550630
39.8883: v.resize(12582912) time : 556319
40.9711: v.resize(12582912) time : 530739
41.5182: v.resize(0) time : 546654
42.0733: v.resize(12582912) time : 554924
43.1321: v.resize(12582912) time : 511659
43.6802: v.resize(0) time : 547084
44.2373: v.resize(12582912) time : 557001
45.3201: v.resize(12582912) time : 530313

在@Snild Dolkow的帮助下,我这次成功地将时间限制为4毫秒

and with help of @Snild Dolkow I succesfully dicline this time to 4 milliseconds

E/READFILE 1: v.resize(12582912) time : 573
E/READFILE 2: v.resize(0) time : 0
E/READFILE 3: v.resize(12582912) time : 4683
E/READFILE 1: v.resize(12582912) time : 557
E/READFILE 2: v.resize(0) time : 0
E/READFILE 3: v.resize(12582912) time : 4680
E/READFILE 1: v.resize(12582912) time : 552
E/READFILE 2: v.resize(0) time : 0
E/READFILE 3: v.resize(12582912) time : 4683

我刚刚将此行添加到了CMakeList.txt文件

I just added this lines in my CMakeList.txt file

target_compile_options(native-lib PRIVATE
    "$<$<CONFIG:RELEASE>:-O3>"
    "$<$<CONFIG:DEBUG>:-O3>")

但是我意识到无论如何您现在可以在第二个日志中看到的时间现在确实是合乎逻辑的……这里有些奇怪的事情正在发生.

But I realized that anyway the time that you can see in a second log, is now really logical... Something weird is going on here.

看一下-第一次分配需要552微秒,然后将大小调整为0需花费0毫秒(可以),但实际上将大小调整为与第一次调整大小相同的最后一次调整需要4600微秒.

Take a look - first allocation take 552 microseconds, then resize to 0 take 0 milliseconds(it is ok), but last resize that is actually resize to the same size that was in first resize take 4600 microseconds.

这是不可能的,因为向量已经过调整大小,并且当我将大小调整为0时,唯一改变的值是正确地计算内部元素的数量,因此再次调用调整大小为之前的数字意味着只需更改计数即可在向量实现内部,换句话说,应该不超过0微秒...

It is could not be possible because the vector have already was resized and only value that changing when I call resize to 0 is acctually count of elements inside, so call again resize to number that was before it is means just change the count inside the vector implementation, in other words it should take not more that 0 microseconds...

所以,问题是-真的是ndk错误吗?或者,我在这里想念什么?

So, question is - is it really ndk bug? Or, I miss something here?

推荐答案

您会遗漏std::vector<T>::resize(N, T defaultVal=T{})将所有N个值都设置为defaultVal.如果未指定默认值,则默认值为T{}.在您的情况下,该值为零.

You're missing that std::vector<T>::resize(N, T defaultVal=T{}) sets all N values to defaultVal. If you don't specify a default value, the default is T{}. In your case, that's zero.

仅分配而不影响值的方法称为std::vector::reserve(N).相关功能是std::vector::capacity()std::vector::shrink_to_fit()

The method which only does allocation, and does not affect values is called std::vector::reserve(N). Related functions are std::vector::capacity() and std::vector::shrink_to_fit()

这篇关于为什么即使在优化级别为3的情况下,向量分配也要花费这么多时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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