STL向量中隐藏的副本?(hidden copy in STL vector?)
使用带类的向量时,我遇到意外的性能下降。
我有一个csv_File类,它读入一个csv文件并将内容存储在一个2D向量中。 有一个允许访问的成员函数,例如
csv_File file("file.csv"); file.access(2,2);
访问2,2元素。
然后,我有另一个类csv_Array,它在向量中存储多个csv_File对象,例如私有成员向量有一个允许访问的成员函数,即它返回一个csv_File对象,例如:
csv_Array file_array(5); //store 5 csv_File objects file_array.grab(0).access(2,2);
在第二行中,grab返回一个csv_File对象(在本例中是第一个),access是csv_File对象的成员函数。
但是,我注意到调用csv_Array.grab(0).access(2,2); 比它应该慢得多(它应该只是3个vector :: at calls)。
是否有某种隐藏的副本正在使这个非常慢?
编辑:以下是一些相关的函数原型:
//Access in csv_File std::string access(int row, int column); //grab in csv_Array and csv_Analysis (mentioned below) csv_File grab(int index);
附加详细信息(如有必要):此代码的应用是将一堆csv文件加载到内存中,然后将其传递给将对数据进行一些计算的朋友类。 原理上,我有以下内容:
1)csv_Array具有私有成员向量存储; 2)csv_Analysis是一个csv_Array的朋友的类3)csv_Analysis访问矢量存储,它位于csv_Array中4)这种访问是通过将csv_Analysis传递给csv_Array中的存储(因此没有希望复制....)来完成的,例如public:csv_Analysis(csv_Array&csv_block);
因此,上面给出的调用[file_array.grab(0).access(2,2); ]实际上还有一个额外的“级别”,更像是
csv_Analysis analysis_Object(file_array); analysis_Object.grab(0).access(2,2);
其中grab以相同的方式起作用,并且也被定义为csv_Analysis类的成员函数。
I am experiencing unexpected performance degradation when using vectors with classes.
I have a csv_File class that reads in a csv file and stores the contents in a 2D vector. There's a member function that allows access, e.g.
csv_File file("file.csv"); file.access(2,2);
To access the 2,2 element.
Then, I have another class csv_Array that stores multiple csv_File objects in a vector, e.g private member vector There's a member function that allows access, i.e. it returns a csv_File object, for example:
csv_Array file_array(5); //store 5 csv_File objects file_array.grab(0).access(2,2);
In the second line, grab returns a csv_File object (in this case, the first one) and access is a member function of the csv_File object.
However, I have noticed that the call csv_Array.grab(0).access(2,2); is much slower than it should be (it should be just 3 vector::at calls).
Is there some sort of hidden copy going on that is making this very slow?
EDIT: Here are some of the relevant function prototypes:
//Access in csv_File std::string access(int row, int column); //grab in csv_Array and csv_Analysis (mentioned below) csv_File grab(int index);
ADDITIONAL DETAILS (if necessary): The application of this code is to load a bunch of csv files into memory before passing it to a friend class that will do some calculations with the data. Schematically, I have the following:
1) csv_Array has private member vector storage; 2) csv_Analysis is a class that is a friend of csv_Array 3) csv_Analysis accesses vector storage, which is in csv_Array 4) This access is done by passing csv_Analysis a reference to storage in csv_Array (so no copy hopefully....), e.g public: csv_Analysis(csv_Array &csv_block);
Thus, the call given above[file_array.grab(0).access(2,2); ] actually has one additional class "level" in between and is more like
csv_Analysis analysis_Object(file_array); analysis_Object.grab(0).access(2,2);
where grab acts in the same way, and is also defined as a member function of csv_Analysis class.
原文:https://stackoverflow.com/questions/6514438
最满意答案
如果你愿意,你可以使用字符串进行记录,以“包裹”你的实际信号。 这是一个小小的问题,但我很难想到另一种方式,没有头脑伤害魔法的文件在VHDL中更糟糕。 所以:
type stdWithName is record name : String; value : std_logic; end record;
然后你可以初始化它:
signal val : std_logic := '0'; signal withName : stdWithName := ("withName",val);
并比较名称
if (withName.name = "withName") then -- huzzah! a match. end if;
If you wanted, you could make a record with strings to 'wrap' around your actual signal. This is a little kludgey, but it is hard for me to think of another way, short of head-hurting magic with files that would be even worse in VHDL. So:
type stdWithName is record name : String; value : std_logic; end record;
you could then initialize this like:
signal val : std_logic := '0'; signal withName : stdWithName := ("withName",val);
and compare the name like
if (withName.name = "withName") then -- huzzah! a match. end if;
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这个问题可能没有用最好的方式表达出来。 我最终提出了一个可能有点笨拙的解决方案,但它的工作原理是: strings: - match: '"' scope: punctuation.definition.string.begin.eviews push: string string: - meta_scope: string.quoted.double.eviews - match: '""' - match: '"' scope: punctuation.defi ...
-
是否存在Android ViewAttachedToActivity事件?(Existance of Android ViewAttachedToActivity event?)[2021-12-23]
我不认为这样的事件存在 I dont think an event like this exists -
今天早上我运行了自动服务器更新软件( 仅更新了服务器的插件)并重新启动了Apache。 Apache没有更新(所以仍然是相同的版本),但各种插件已更新,重新启动Apache后,问题消失了。 我仍然不知道是什么造成了Apache这种明显的混乱。 This morning I ran automatic server update software (updating plugins for the server only) and rebooted Apache. Apache did not update ...
-
虽然没有推荐,但可以使用eval() 。 查看: >>> data = {'a': 3, 'b': 4} >>> eval('{a} + {b}'.format(**data)) >>> 7 eval()将尝试以python代码的形式执行给定的字符串。 有关python format更多信息,您可以查看一下非常好的pyformat站点。 Although not recomended, you can use eval(). Check out: >>> data = {'a': 3, 'b': 4} > ...
-
如果你愿意,你可以使用字符串进行记录,以“包裹”你的实际信号。 这是一个小小的问题,但我很难想到另一种方式,没有头脑伤害魔法的文件在VHDL中更糟糕。 所以: type stdWithName is record name : String; value : std_logic; end record; 然后你可以初始化它: signal val : std_logic := '0'; signal withName : stdWithName := ("wi ...
-
这有点令人困惑,因为如果要检查特定文件夹中是否存在文件夹/文件或文件,则会涉及很多hasNext 。 我通过反复试验得到了这个。 hasNext() 确定调用next()是否将返回一个项目。 返回 Boolean - 如果next()将返回一个项,则为true ; 如果不是false 首先是一些相关的SO问题: 如何检查Google云端硬盘中是否存在(按名称)文件? Google Apps脚本:检查上传文件是否存在或为空 这是一个示例代码: function checkFile(filename){ v ...
-
使用mysqli prepared语句测试表中某行的存在情况(testing for existance of a row in a table using mysqli prepared statement)[2022-05-25]
您必须在执行查询后检索结果才能处理结果。 添加$stmt->store_result(); 在execute但在使用num_rows之前。 You have to retrieve the result after executing the query in order to work with the results. Add $stmt->store_result(); after the execute but before you use num_rows. -
您正在使用.text() ,它将始终对提供的文本进行html编码...使用.html()代替 $("
- ").html(post + "
" + month + "/" + day + "/" + year).prependTo(".posts"); You are using .text() which will ALWAYS html-encode the text provided...use .html() instead $("- ").html(post + "
" + m ... - ").html(post + "