On Wed, Feb 17, 2010 at 12:19 AM, Romain Francois <
[email protected]> wrote:

> On 02/17/2010 12:51 AM, Kenton Varda wrote:
>
>> The Reflection interface already provides a way -- FieldSize() and
>> GetRepeatedX().  The only problem is that it's a bit slower than the
>> generated accessors because these methods aren't inlineable.
>>
>
> Sure. I meant "STL algorithms" iterating.


You could easily write an STL-like iterator on top of these if you really
need it.


>
>  BTW, have you observed an actual performance problem or are you just
>> speculating that this performance difference may be a problem for you?
>>
>
> In similar (non protobuf-related) settings, we have observed quite a bit of
> difference between using plain loops and accessors as opposed to iterators.
>

It would obviously depend on the data structure involved.  For example, on
an stl vector, the following two loops will have equivalent performance:

  vector<int> v;
  for (int i = 0; i < v.size(); ++i) {
    DoSomehting(v[i]);
  }

  for (vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
    DoSomehting(*i);
  }

In fact, a good compiler may even produce identical assembly code for both
loops.


> But this is mainly speculation on the performance.
>

It's generally a bad idea to try to fix theoretical performance problems.

But when you use std::vector, it is best to first reserve the target size as
> opposed to create an empty vector and push_back each element.
>

It's slightly faster, but either way is still O(n).  Reserving is often not
worth the effort, especially if you are good about reusing objects, in which
case they will already have space reserved from the previous use.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to