分析一些代码 [英] Analyzing some piece of code

查看:90
本文介绍了分析一些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种行事方式吗?



  void  CC :: read(std :: vector< SomeStructure>& info,Blob& data  / *   This只是一些二进制数据imho * / 
{


SomeStructure * ptr =(SomeStructure *)data.data();


for int i = 0 ; i< data.size()/ sizeof(SomeStructure); i ++)
{

info.push_back(* ptr);


ptr ++;
}
}

解决方案

您的问题的答案在于Blob类的定义。代码看起来很可疑,但是你在这里展示的内容看起来完全没错。要完成这个问题,需要提供Blob的详细信息。



如果Blob实际上由一个连续的SomeStructure数组组成,那么你的问题就是错误的。您可能会考虑一些对齐细节。 Blob中的连续元素可能会有一些你没有考虑的填充。这个细节是由编译器决定的,无法可靠地预测。



在任何你试图超越编译器的情况下,你通常会输掉。

在我看来,这是一段效率低下的代码。据我了解,它应该执行以下操作:



Blob对象包含已知长度的二进制数据,它是SomeStucture长度的倍数。将整个数据块复制到元素类型为SomeStructure的向量中。



代码效率低下有两个原因:



(a)SomeStructure的向量是逐个元素生长的。这需要在分配的空间耗尽时在向量内部进行多次大型复制操作。



(b)使用SomeStructure元素作为参数执行操作push_back,这需要(取决于编译器优化)多个复制操作,直到数据到达向量。



分配所需的向量大小会更有效率只需一个操作然后用一个memcpy复制整个blob。



所有这一切都假设Blob对象的二进制格式真的与之兼容SomeStructure数组。如果该代码在我的项目中,我会试图摆脱它。


Is this an OK way to do what it does?

void CC::read(std::vector<SomeStructure> &info, Blob &data /*This is just some binary data imho*/)
{

 
   SomeStructure *ptr=(SomeStructure*) data.data();

 
    for(int i= 0;i<data.size()/sizeof(SomeStructure);i++)
    {
 
        info.push_back(*ptr);

 
        ptr++;
    }
}

解决方案

The answer to your question lies in the definition of the Blob class. The code looks suspect but nothing in what you have presented here looks completely wrong. To complete the question, the details of Blob need to be provided.

If Blob actually consists of a contiguous array of SomeStructure, you are going about the problem wrong. There will likely be some alignment details you are not considering. Consecutive elements in Blob will likely have some padding that you are not considering. This detail is determined by the compiler and can't be reliably predicted.

In any situation where you try to outsmart the compiler, you will usually lose.


In my opinion this is an inefficient piece of code. As far as I understand it, it is supposed to do the following:

The Blob object contains binary data of known length which is a multiple of the length of SomeStucture. This entire chunk of data is copied into a vector with element type SomeStructure.

The code is inefficient for two reasons:

(a) The vector of SomeStructure is grown element by element. That requires multiple big copy operations that take place inside vector whenever the allocated space is exhausted.

(b) The operation push_back is executed with an element of SomeStructure as argument, which requires (depending on compiler optimization) multiple copy operations until the data arrives in the vector.

It would have been much more efficient to allocate the required size of the vector with a single operation and then to copy the entire blob with a single memcpy.

And all that under the assumption that the binary format of the Blob object is really compatible to an array of SomeStructure. If that code was in my project I would try to get rid of it.


这篇关于分析一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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