473,322 Members | 1,403 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,322 software developers and data experts.

fastest way of storing structured data

Hi,

I am working on a project involving contactless card and to read or
write into these cards we need some parameters (Key to use for instance).
My problem is to store these parameters in the most efficient way.

I was thinking first in a c manner as a struct array like this :
struct TFileInfo
{
int nSFID, // Short file ID
int nLID, // Long File ID
int nKey
};
// not sure of the syntax, it's been a long time I haven't used it
TFileInfo fileInfo[] =
{
{ 0x17, 0x3127, 0x0E },
{ 0x18, 0x3128, 0x08 },
};
and after I need to be able to get the values associated to nSFID.
ex :
CMyClass::GetKey(int nSFID)
{
for (int i = 0; i < nItems; i++){
if (fileInfo[i].nSFID == nSFID)
return fileInfo[i].nKey
}
}
Or another solution would be to use a std::map<int, TFileInFo>
but I am concerned about performance. Besides I don't find the
initialization very elegant

std::map<int, TFileInfo> fileInfo;

TFileInfo stFileInfo;

stFileInfo.nSFID = 0x17;
stFileInfo.nLID = 0x3127;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

stFileInfo.nSFID = 0x18;
stFileInfo.nLID = 0x3128;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

I must add that my numbers of items will be 20 max.
If someone could inform me about this performance problem...

Aug 30 '05 #1
6 1764

Vince wrote:
Hi,

I am working on a project involving contactless card and to read or
write into these cards we need some parameters (Key to use for instance).
My problem is to store these parameters in the most efficient way.

I was thinking first in a c manner as a struct array like this :
struct TFileInfo
{
int nSFID, // Short file ID
int nLID, // Long File ID
int nKey
};
// not sure of the syntax, it's been a long time I haven't used it
TFileInfo fileInfo[] =
{
{ 0x17, 0x3127, 0x0E },
{ 0x18, 0x3128, 0x08 },
};
and after I need to be able to get the values associated to nSFID.
ex :
CMyClass::GetKey(int nSFID)
{
for (int i = 0; i < nItems; i++){
if (fileInfo[i].nSFID == nSFID)
return fileInfo[i].nKey
}
}

this will take an average of 10 comparisions to find the value, if
there's 20 elements. A std::map will take an average of 4 comparisions.
And so, which is more efficient? A linear search is O(n) while a search
on a std::map is O(log n).

Or another solution would be to use a std::map<int, TFileInFo>
but I am concerned about performance. Besides I don't find the
initialization very elegant

std::map<int, TFileInfo> fileInfo;

TFileInfo stFileInfo;

stFileInfo.nSFID = 0x17;
stFileInfo.nLID = 0x3127;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

stFileInfo.nSFID = 0x18;
stFileInfo.nLID = 0x3128;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

I must add that my numbers of items will be 20 max.
If someone could inform me about this performance problem...


you might want to consider a std::set instead, since the key is
embedded into the TFileInfo struct. Then give your struct a an
operator<, or pass a functor as the 2nd template argument to std::set.

In any case, you can initialize a std::set or std::map with an array:

std::set<TFileInfo> myset(fileInfo, fileInfo + 20); // where fileInfo
is the array you have above

A std::map expects iterators to std::pairs, which makes the
initialization a little more awkward:

// you must give TFileInfo a constructor
std::pair<int, TFileInfo> init[] =
{ std::make_pair(0x17, TFileInfo(0x17, 0x3127, 0x0E)),
std::make_pair(... , TFileInfo( ... )),
...
};

std::map<int, TFileInfo> mymap(init, init + 20);

Aug 30 '05 #2
Vince wrote:
CMyClass::GetKey(int nSFID)
{
for*(int*i*=*0;*i*<*nItems;*i++){
if (fileInfo[i].nSFID == nSFID)
return fileInfo[i].nKey
}
}


If you sort your array 'fileInfo' by the nSFID field, then
you can have O(logN) lookup by using std::lower_bound().

There's nothing wrong per se with POD arrays, esp. if they
can be const, and thus be put into read-only memory.

Marc

Aug 30 '05 #3
"Vince" <vs**@caramail.com> wrote in message
news:43***********************@news.free.fr...
Hi,

I am working on a project involving contactless card and to read or
write into these cards we need some parameters (Key to use for instance).
My problem is to store these parameters in the most efficient way.

I was thinking first in a c manner as a struct array like this :
struct TFileInfo
{
int nSFID, // Short file ID
int nLID, // Long File ID
int nKey
};
// not sure of the syntax, it's been a long time I haven't used it
TFileInfo fileInfo[] =
{
{ 0x17, 0x3127, 0x0E },
{ 0x18, 0x3128, 0x08 },
};
and after I need to be able to get the values associated to nSFID.
ex :
CMyClass::GetKey(int nSFID)
{
for (int i = 0; i < nItems; i++){
if (fileInfo[i].nSFID == nSFID)
return fileInfo[i].nKey
}
}
Or another solution would be to use a std::map<int, TFileInFo>
but I am concerned about performance. Besides I don't find the
initialization very elegant

std::map<int, TFileInfo> fileInfo;

TFileInfo stFileInfo;

stFileInfo.nSFID = 0x17;
stFileInfo.nLID = 0x3127;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

stFileInfo.nSFID = 0x18;
stFileInfo.nLID = 0x3128;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

I must add that my numbers of items will be 20 max.
If someone could inform me about this performance problem...


Performance questions are best answered by performance tests. Since there's
only 20 items, I'd bet that the difference between the array and std::map
wouldn't be significant unless your application performs this lookup very
frequently.

As Marc pointed out, sorting the entries and using a search routine is
another option. Also, if nSFID has a limited range, you could create an
array that can be indexed by the nSFID, giving you O(1) lookup time (but
possibly requiring a little more memory).

--
David Hilsee
Aug 30 '05 #4
Vince wrote:

Others have already talked about your 'performance' issue.

But ...
Besides I don't find the
initialization very elegant

std::map<int, TFileInfo> fileInfo;

TFileInfo stFileInfo;

stFileInfo.nSFID = 0x17;
stFileInfo.nLID = 0x3127;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;

stFileInfo.nSFID = 0x18;
stFileInfo.nLID = 0x3128;
stFileInfo.nKey = 0x0E;
fileInfo[0x17] = stFileInfo;


.... this can easily be cured.
Just provide your struct with a constructor:

struct TFileInfo
{
TFileInfo( int SFID, int LID, int Key )
: nSFID( SFID ), nLID( LID ), nKey( Key )
{}

int nSFID, // Short file ID
int nLID, // Long File ID
int nKey
};

and now you can ceasily create objects and initialize them
on the fly:

fileInfo[0x17] = TFileInfo( 0x17, 0x3127, 0x0E );
fileInfo[0x18] = TFileInfo( 0x18, 0x3128, 0x0E );

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 31 '05 #5
Alipha wrote:
Vince wrote:
Hi,

I am working on a project involving contactless card and to read or
write into these cards we need some parameters (Key to use for instance).
My problem is to store these parameters in the most efficient way.

I was thinking first in a c manner as a struct array like this :
struct TFileInfo
{
int nSFID, // Short file ID
int nLID, // Long File ID
int nKey
};


you might want to consider a std::set instead, since the key is
embedded into the TFileInfo struct. Then give your struct a an
operator<, or pass a functor as the 2nd template argument to std::set.


But how would you get the TFileInfo structure you're looking for? If I
want to use set::find(), I need to pass a struct TFileInfo, but I just
have a nSFID.

Mark
Aug 31 '05 #6

Capstar wrote:
[snip]

But how would you get the TFileInfo structure you're looking for? If I
want to use set::find(), I need to pass a struct TFileInfo, but I just
have a nSFID.

Mark


create a TFileInfo with the nSFID field set to what you want then. If
you give your struct a one-argument ctor (probably explicit), then it
would be easy to do myset.find(TFileInfo(0x17)). Of course, you lose
the ability to initialize your struct with { } initializers.

Sep 1 '05 #7

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...
3
by: _link98 | last post by:
Running DB2 ESE V8.1.8 on WinXP. This is Fixpak 8. Have a structured-type and some methods for that type. One of my methods needs to do insert / update on tables. The type specification...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
5
by: Loui Mercieca | last post by:
Hi, In my design i have a data structure used to store large amount of numbers ( in the range of lots of thousands ). Each element contains 3 items and the no of elements are dynamic.. of the...
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...
4
by: Dharmesh | last post by:
Hi every1, I am learning ASP.NET and came accross problem of storing my classes object and I want its scope to be request. Is there a way do that. I found the only way to store structured values...
6
by: Mudcat | last post by:
Hi, I am trying to build a tool that analyzes stock data. Therefore I am going to download and store quite a vast amount of it. Just for a general number - assuming there are about 7000 listed...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
3
by: Mohamed Yousef | last post by:
Thanks all , but there is still something i forget to state -sorry - all communication will be via Http with a server so data is received via Http so local network solutions won't work the...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...

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.