473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Fastest way to construct this class

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
2 1630
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
44
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas...
9
by: sshock | last post by:
Hi all, I want to read from a file into a vector<unsigned char>. Right now my code looks like this: FILE* f = fopen( "datafile", "rb" ); enum { SIZE = 100 }; vector<unsigned char>...
28
by: Ilias Lazaridis | last post by:
I understand that I can use __metaclass__ to create a class which modifies the behaviour of another class. How can I add this metaclass to *all* classes in the system? (In ruby I would alter...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
1
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
5
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary>...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.