473,811 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 EquityTradeReco rd : public Record {

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

public:
EquityTradeReco rd (char *rec);

};

EquityTradeReco rd::EquityTrade Record (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 1655
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 EquityTradeReco rd : public Record {

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

public:
EquityTradeReco rd (char *rec);

};

EquityTradeReco rd::EquityTrade Record (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 EquityTradeReco rd : public Record {

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

public:
EquityTradeReco rd (char *rec);

};

EquityTradeReco rd::EquityTrade Record (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
5496
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 a list of perhaps 50 ip addresses to be used in a packet sniffing application. For each packet that goes through the application (which will be monitoring all traffic through a switch), I need to see if an entry for the source ip of that packet...
11
3628
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 million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
44
3236
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 others said C++/CLI was more optimized. Anyway, I wrote up some very simple test code, and at least on my computer C++/CLI came out the fastest. Here's the sample code, and just for good measure I wrote one in java, and it was the slowest! ;-)...
9
6829
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> buf(SIZE); fread(&buf, 1, SIZE, f);
28
1890
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 the "Class" class) .. http://lazaridis.com
3
1794
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 string arrays, which are just lists of people or companies in alphabetic order. These names may have accented and umlauted characters (which are present as the plain ASCII - not as the entity &# character). The page is UTF-8 encoded. e.g. ...
1
2436
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 string arrays, which are just lists of people or companies in alphabetic order. These names may have accented and umlauted characters (which are present as the plain ASCII - not as the entity &# character). The page is UTF-8 encoded. e.g. ...
5
3272
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> /// Create an object of the given type /// A default constructor is required to be successful. /// A failure will return null. /// </summary>
22
2720
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 the following methods must be implemented: insert(), find(), begin(), end(), erase(), size(), operator<(), and at least the forward iterator. Here, speed and correctness are the 2 most important factors. Functionally it should behave similar to...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.