Fastest way to construct this class

N

Ninan

I have different 256 character length records stored in a file. The
first character is the record identifier. I want to instantiate
different record objects. I cannot change the records stored in file as
it comes from a third party standard. I am planning to use the factory
method to create the objects.
Is there a faster way to construct this class other than the obvious
solution I am presenting below

class Record {

};

class EquityTradeRecord : public Record {

char OpId;
char TradeRefNo[12];
char SecurityId [12];
char Internal RefNo [9];
char BrokerId [9];
----------- and so on

public:
EquityTradeRecord (char *rec);

};

EquityTradeRecord::EquityTradeRecord (char *rec)
{
//First character record Id and not stored
OpId = rec[1];
strncpy (TradeRefNo, rec+2, 11);
TradeRefNo[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (TradeRefNo, ' '))
tmp[0] = '\0';
strncpy (SecurityId, rec+13, 11);
SecurityId[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (SecurityId, ' '))
tmp[0] = '\0';

// and so on

}
 
M

mlimber

Ninan said:
I have different 256 character length records stored in a file. The
first character is the record identifier. I want to instantiate
different record objects. I cannot change the records stored in file as
it comes from a third party standard. I am planning to use the factory
method to create the objects.
Is there a faster way to construct this class other than the obvious
solution I am presenting below

class Record {

};

class EquityTradeRecord : public Record {

char OpId;
char TradeRefNo[12];
char SecurityId [12];
char Internal RefNo [9];
char BrokerId [9];
----------- and so on

public:
EquityTradeRecord (char *rec);

};

EquityTradeRecord::EquityTradeRecord (char *rec)
{
//First character record Id and not stored
OpId = rec[1];
strncpy (TradeRefNo, rec+2, 11);
TradeRefNo[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (TradeRefNo, ' '))
tmp[0] = '\0';
strncpy (SecurityId, rec+13, 11);
SecurityId[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (SecurityId, ' '))
tmp[0] = '\0';

// and so on

}

Nothing faster, but you might consider using C++ strings/stringstreams
instead of C-style strings and functions. C-style strings are
inherently fragile, and strncpy and similar functions are more
error-prone (e.g., compare when strncpy does or does not append a null
terminator). You might also be interested in the serialization FAQs:

http://www.parashift.com/c++-faq-lite/serialization.html

and perhaps in Boost's serialization library:

http://boost.org/libs/serialization/doc/index.html

Cheers! --M
 
A

Alf P. Steinbach

* Ninan:
I have different 256 character length records stored in a file. The
first character is the record identifier. I want to instantiate
different record objects. I cannot change the records stored in file as
it comes from a third party standard. I am planning to use the factory
method to create the objects.
Is there a faster way to construct this class other than the obvious
solution I am presenting below

class Record {

};

class EquityTradeRecord : public Record {

char OpId;
char TradeRefNo[12];
char SecurityId [12];
char Internal RefNo [9];
char BrokerId [9];
----------- and so on

public:
EquityTradeRecord (char *rec);

};

EquityTradeRecord::EquityTradeRecord (char *rec)
{
//First character record Id and not stored
OpId = rec[1];
strncpy (TradeRefNo, rec+2, 11);
TradeRefNo[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (TradeRefNo, ' '))
tmp[0] = '\0';
strncpy (SecurityId, rec+13, 11);
SecurityId[11] = '\0';
//The fields are space padded and no null terminations
if (char *tmp = strchr (SecurityId, ' '))
tmp[0] = '\0';

// and so on

}

Store the entire 256 character string as a whole, and use member
functions to access parts of it.

If by /measuring/ the performance you determine that e.g. conversion
from part to std::string is a bottleneck, then think about introducing
e.g. 'mutable' cache member variables, caching a part's converted
representation the first time it's accessed (if ever).

Without affecting the client code other than wrt. performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top