By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Fastest way to construct this class

P: n/a
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

}

Apr 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ninan wrote:
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-lit...alization.html

and perhaps in Boost's serialization library:

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

Cheers! --M

Apr 18 '06 #2

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.