473,698 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fasteste way to fill a structure.

From my previous post...

If I have a structure,

struct sFileData
{
char*sSomeStrin g1;
char*sSomeStrin g2;
int iSomeNum1;
int iSomeNum2;
sFileData(){... };
~sFileData(){.. .};
sFileData(const sFileData&){... };
const sFileData operator=( const sFileData &s ){...}
};

I read the file as follows

FILE *f = fopen( szPath, "rb" );

int nLineSize = 190;
BYTE b[nLineSize+1];

fread( b, sizeof(BYTE), nLineSize, f );
int numofrecords = atoi( b ); // first line is num of records only,

// read the data itself.
while( fread( b, sizeof(BYTE), nLineSize, f ) == nLineSize )
{
// fill data
// The locations of each items is known
// sString1 = 0->39, with blank spaces filler after data
// sString2 = 40->79, with blank spaces filler after data
// iNum1 = 80->99, with blank spaces filler after data
// iNum2 = 100->end, with blank spaces filler after data
}

what would be the best way to fill the data into an array, (vector)?

Many thanks.

Simon.
Jul 23 '05
21 6651
Larry I Smith wrote:
simon wrote:
On my pc (an old Gateway PII 450MHZ with 384MB of RAM):

simon.cpp runs in 2.20 seconds and uses 5624KB of memory.


Thanks for that, I get 1.24 sec and 6mb.
I just need to check what the difference is with my code.
simon2.cpp runs in 2.22 seconds and uses 6272KB of memory.

Your mileage may vary. I'm running SuSE Linux v9.3 and
using the GCC "g++" compiler v3.3.5.

Regards,
Larry
Here are the 3 programs:


<snip code>
Regards,
Larry


Thanks for that, this is great.
I wonder if my Trim(...) function was not part of the problem.

After profiling I noticed that delete [], (or even free(..) ) takes
around 50% of the whole time.

Maybe I should get rid of the dynamic allocation all together.

Simon


What does your profiler say about simon2.cpp?

Actually 1.24 seconds is pretty good for 100000 records.

As far as the memory usage goes, did you read the 2
articles on malloc that I posted earlier? Whether you
use new/delete or std::string (which does its own new/delete
behind the scenes) doesn't make much difference in performance
or memory usage, but std::string allows you much more
flexibility when manipulating the strings after you've
filled your vector (i.e. later in the program).

Due to the many (200000) tiny memory allocations, your memory
usage would be about:

2.5 * (sizeof(sFiledD ata) * 100000)

when both strings (sSomeString1 & sSomeString2) are small.

16 bytes minimum (plus the pointer kept in sFileData) will
be allocated for each of those strings. So, using pointers
in sFileData, the actual memory used for one sFiledData
is at least 48 bytes.

Regards,
Larry


What am I missing here? There must be some part of the problem that I
missed. The following reads in 100000 data paris, one per line, in far
less than a second.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <iterator>

using namespace std;

template<typena me Key_T, typename Value_T>
struct Data {
Key_T _key;
Value_T _value;

istream& fromStream(istr eam& in) {
return in >> _key >> _value;
}

ostream& toStream(ostrea m& out) const {
ios::fmtflags oldFlags(out.se tf(ios::left));
out << setw(25) << _key << _value;
out.setf(oldFla gs);
}

};

template<typena me Key_T, typename Value_T>
istream& operator>>(istr eam& in, Data<Key_T, Value_T>& data) {
return data.fromStream (in);
}

template<typena me Key_T, typename Value_T>
ostream& operator<<(ostr eam& out, const Data<Key_T,Valu e_T>& data) {
return data.toStream(o ut);
}

int main(int argc, char* argv[]) {
if(!(argc > 1)) {
cerr << "records filename: " << endl;
return -1;
}

ifstream ifs(argv[1]);

if(!ifs.is_open ()) {
cerr << "Failed to open file: " << argv[1] << endl;
return -1;
}

typedef Data<string, string> D_T;
typedef vector<D_T> DV_T;
DV_T dv;

copy(istream_it erator<D_T>(ifs ),istream_itera tor<D_T>(),back _inserter(dv));
// copy(dv.begin() , dv.end(),ostrea m_iterator<D_T> (cout,"\n"));
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #21
Steven T. Hatton wrote:
Larry I Smith wrote:
simon wrote:
>On my pc (an old Gateway PII 450MHZ with 384MB of RAM):
>
> simon.cpp runs in 2.20 seconds and uses 5624KB of memory.
Thanks for that, I get 1.24 sec and 6mb.
I just need to check what the difference is with my code.

> simon2.cpp runs in 2.22 seconds and uses 6272KB of memory.
>
>Your mileage may vary. I'm running SuSE Linux v9.3 and
>using the GCC "g++" compiler v3.3.5.
>
>Regards,
>Larry
Here are the 3 programs:

<snip code>

Regards,
Larry
Thanks for that, this is great.
I wonder if my Trim(...) function was not part of the problem.

After profiling I noticed that delete [], (or even free(..) ) takes
around 50% of the whole time.

Maybe I should get rid of the dynamic allocation all together.

Simon

What does your profiler say about simon2.cpp?

Actually 1.24 seconds is pretty good for 100000 records.

As far as the memory usage goes, did you read the 2
articles on malloc that I posted earlier? Whether you
use new/delete or std::string (which does its own new/delete
behind the scenes) doesn't make much difference in performance
or memory usage, but std::string allows you much more
flexibility when manipulating the strings after you've
filled your vector (i.e. later in the program).

Due to the many (200000) tiny memory allocations, your memory
usage would be about:

2.5 * (sizeof(sFiledD ata) * 100000)

when both strings (sSomeString1 & sSomeString2) are small.

16 bytes minimum (plus the pointer kept in sFileData) will
be allocated for each of those strings. So, using pointers
in sFileData, the actual memory used for one sFiledData
is at least 48 bytes.

Regards,
Larry


What am I missing here? There must be some part of the problem that I
missed. The following reads in 100000 data paris, one per line, in far
less than a second.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <iterator>

using namespace std;

template<typena me Key_T, typename Value_T>
struct Data {
Key_T _key;
Value_T _value;

istream& fromStream(istr eam& in) {
return in >> _key >> _value;
}

ostream& toStream(ostrea m& out) const {
ios::fmtflags oldFlags(out.se tf(ios::left));
out << setw(25) << _key << _value;
out.setf(oldFla gs);
}

};

template<typena me Key_T, typename Value_T>
istream& operator>>(istr eam& in, Data<Key_T, Value_T>& data) {
return data.fromStream (in);
}

template<typena me Key_T, typename Value_T>
ostream& operator<<(ostr eam& out, const Data<Key_T,Valu e_T>& data) {
return data.toStream(o ut);
}

int main(int argc, char* argv[]) {
if(!(argc > 1)) {
cerr << "records filename: " << endl;
return -1;
}

ifstream ifs(argv[1]);

if(!ifs.is_open ()) {
cerr << "Failed to open file: " << argv[1] << endl;
return -1;
}

typedef Data<string, string> D_T;
typedef vector<D_T> DV_T;
DV_T dv;

copy(istream_it erator<D_T>(ifs ),istream_itera tor<D_T>(),back _inserter(dv));
// copy(dv.begin() , dv.end(),ostrea m_iterator<D_T> (cout,"\n"));
}


Each of his 100000 records (each 192 bytes long) contains multiple
fields that must be parsed out of the record, then have lead/trail
blanks trimmed; additional fields in the record must be parsed out
and converted to int. Each record also contains fields that are to
be skipped over (i.e. ignored). Once all of the fields are parsed
out of a record, an object of class sFileData is constructed using
the data parsed from the record; then that sFileData object is
put into the vector. Only after all of this is the next record
read from the file. So most of the work is data parsing.

Larry
Jul 23 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2409
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to follow? Second, have I correctly passed the structure's pointer to the functions in this program?
0
2239
by: Kirk Marple | last post by:
i have a large C++ data structure that i'm trying to interop with... the structure is 77400 bytes long. i have the structure defined in C#, so i was trying to just use "ref <structure>" as the method parameter. if i use this direct approach, i get the error: Message: Cannot marshal 'parameter #1': Internal limitation: structure is too complex or too large." is there a 64k limit on marshalling structures? what workarounds are there...
10
2122
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or multiple arrays,etc) I do not understand how to define the structure at run time.i.e.what fields it will contain.
2
1040
by: jim_adams | last post by:
For a nested structure such as: Dim userVariable as one structure one dim a as string dim b() as two end structure structure two
10
4990
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
11
1922
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t Public mGlobalRect As GM_Rectangle_t Public mPixelWidth As Int32
24
4435
by: oliv29 | last post by:
Hello, Can anyone tell me how do i fill a structure's fields in a for loop ? #define MAX_FIELD_LEN 10 typedef struct { char field1; /*will store first string read from keyboard*/
3
1778
by: Vasu | last post by:
Hi! Can anybody there help me in analysis of the following code, which is a structure of customer's details, asks user to fill in the no. of customers and then their details. When I put in no. of customer as 2 and start filling in users details, in the detail of second customer till the name of State is OK as soon as I fill in the detail of State and press enter, it shows the field of PIN code but then suddenly it says something like...
5
3788
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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
9157
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...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7721
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
5860
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
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.