The code i am using is below:

#include <fstream>
#include <iostream>
#include <string>
#include "addressbook.pb.h"

#define FILENAME "ashu.pb"

using namespace std;

AddressBook address_book;

int getFileSize()
{
    ifstream file(FILENAME, ifstream::in | ifstream::binary);

    if(!file.is_open())
    {
        return -1;
    }

    file.seekg(0, ios::end);
    int fileSize = file.tellg();
    cout << "Size ----- " << fileSize << endl;
    file.close();

    return fileSize;
}

int main()
{
    int x,size;
    Person *p = address_book.add_people();
   
    Person::PhoneNumber* phone_number = p->add_phones();

    fstream out(FILENAME, ios::out | ios::binary | ios::trunc);
    
    for(int j=0;j<5000;j++)
    {  
    p = address_book.add_people();

        p->add_id(j);
    }

    x = address_book.SerializeToOstream(&out);
    out.close();

    size = getFileSize();
    cout << "size " << size << endl;

    AddressBook ad1;     
    fstream in(FILENAME, ios::in | ios::binary);
    ad1.ParseFromIstream(&in);
    
    return 0;
}

and i am using only id field of message Person. Is that the reason why i 
end up getting more size with packed.

On Friday, December 7, 2018 at 12:12:04 PM UTC+5:30, [email protected] 
wrote:
>
> I am trying to find out difference in encoded size using packed equal to 
> true and without any packed encoding. I looped a int32 variable 5000 times. 
> With packed = true, i get size as 29876 and without packed encoding, i get 
> size as 24876. I am confused why i get opposite results. As per my 
> understanding, size with packed should be less as compared to non packed. 
> Can anyone please explain what the issue is?
> My proto file is below for non packed and for packed i modify id filed as; 
> repeated int32 id = 2[packed=true]:
>
> syntax = "proto2";
> message Person {
>   optional string name = 1;
>   repeated int32 id = 2;
>   optional string email = 3;
>
>   enum PhoneType {
>     MOBILE = 0;
>     HOME = 1;
>     WORK = 2;
>   }
>
>   message PhoneNumber {
>     optional string number = 1;
>     optional PhoneType type = 2 [default = HOME];
>   }
>
>   repeated PhoneNumber phones = 4;
> }
>
> message AddressBook {
>   repeated Person people = 1;
> }
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to