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

my code seems slow


Hi,

my code is slow. I have to rethink of it. Can you help?

I'm displaying a logfile from a license server.

I have a vector (m_lines) of
struct LINE
{
STATUS status; // LOGIN or LOGOUT
int hex; // the "user" id
time_t time; // time of login/out
};
which are sorted by time.

Now for each hex's LOGIN there must be a LOGOUT.
If there is none (no idea why), I'll have to add it.

Here's my code so far:

void ForceLogouts()
{
int num = (int)m_lines.size();
for(int i=0; i<num; ++i)
{
LINE& line1 = m_lines[i];
if(line1.status==ST_LOGIN)
{
for(int j=i+1; j<num; ++j)
{
LINE& line2 = m_lines[i];
// good logout
if(line2.hex == line1.hex && line2.status==ST_LOGOUT)
{
goto skipme;
}
}
// this one needs a logout badly!
LINE lt(line1);
tmday = &line1.time + 10000;
lt.status=ST_LOGOUT;
m_lines.push_back(lt);
}
skipme:;
}
// sort(m_lines, ...); // sort in new entries by time
}
What can I do? Sort the data by 'hex' first?


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Sep 11 '06 #1
8 1651
I think you should sort the vector, so when you find a login line at
line i, you just have to check line i++ to see if there's the logout,
otherwise you msyt create it.

Hope it helps!

Paolo
Gernot Frisch ha scritto:
Hi,

my code is slow. I have to rethink of it. Can you help?

I'm displaying a logfile from a license server.

I have a vector (m_lines) of
struct LINE
{
STATUS status; // LOGIN or LOGOUT
int hex; // the "user" id
time_t time; // time of login/out
};
which are sorted by time.

Now for each hex's LOGIN there must be a LOGOUT.
If there is none (no idea why), I'll have to add it.

Here's my code so far:

void ForceLogouts()
{
int num = (int)m_lines.size();
for(int i=0; i<num; ++i)
{
LINE& line1 = m_lines[i];
if(line1.status==ST_LOGIN)
{
for(int j=i+1; j<num; ++j)
{
LINE& line2 = m_lines[i];
// good logout
if(line2.hex == line1.hex && line2.status==ST_LOGOUT)
{
goto skipme;
}
}
// this one needs a logout badly!
LINE lt(line1);
tmday = &line1.time + 10000;
lt.status=ST_LOGOUT;
m_lines.push_back(lt);
}
skipme:;
}
// sort(m_lines, ...); // sort in new entries by time
}
What can I do? Sort the data by 'hex' first?


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
Sep 11 '06 #2
Gernot Frisch wrote:
I have a vector (m_lines) of
struct LINE
{
STATUS status; // LOGIN or LOGOUT
int hex; // the "user" id
time_t time; // time of login/out
};
which are sorted by time.
It would really be better if this was encapuslated.
void ForceLogouts()
{
int num = (int)m_lines.size();
for(int i=0; i<num; ++i)
You should use iterators instead, so that you don't have to change your
code when switching to a different container. And switch you should.
Your algorithm is O(n^2) when it shouldn't take more than O(log n) to
do a search and O(log n) to insert. To do that use an associative
container such as std::map instead of std::vector.
goto skipme;
That's really not necessary. Please read about the 'continue'
statement.
// sort(m_lines, ...); // sort in new entries by time
Do you really want to sort everytime? Use std::map instead.
What can I do? Sort the data by 'hex' first?
Figure out your algorithms and data structures before writing code.

Regards,
Bart.

Sep 11 '06 #3
Bart wrote:
Gernot Frisch wrote:
goto skipme;

That's really not necessary. Please read about the 'continue'
statement.
Oops, that's inside the inner for. My mistake here.

Regards,
Bart.

Sep 11 '06 #4
Your algorithm is O(n^2) when it shouldn't take more than O(log n)
to
do a search and O(log n) to insert. To do that use an associative
container such as std::map instead of std::vector.
I can't use a std::map here, since I _must_ have a vector where I'm
drawing the whole thing. A map would ridiculously drop my drawing
time, which is much more important.

Do you really want to sort everytime? Use std::map instead.
Sorting takes 1 second - no problem.
Figure out your algorithms and data structures before writing code.
I know of std::map and stuff. I know why I had to chose vector.
But making a temporarily std::map for the hex codes and storing a
reference to the timesteps might make sense. Thank you,
-Gernot
Sep 11 '06 #5
In article <4m************@individual.net>, Me@Privacy.net says...

[ ... ]
I'm displaying a logfile from a license server.

I have a vector (m_lines) of
struct LINE
{
STATUS status; // LOGIN or LOGOUT
int hex; // the "user" id
time_t time; // time of login/out
};
which are sorted by time.
I'd build a temporary map with hex as the key, and an int as the value.
When you encounter a LOGIN record, increment the value. When you
encounter a LOGOUT record, decrement the value (and if the value is
zero, you can optionally remove the record). When you're done, each
value in the map tells you how many LOGOUT records need to be added for
that user id.

Right now you're using an O(N*N) algorithm. This is roughly O(N lg N).
If you remove records as they hit zero, it's O(N lg M), where M is the
average number of unmatched LOGIN records over given time as you
traverse the file.

Whether to remove zero records is mostly a question of how often a
specific user is likely to login/out during a run. If they'll login
relatively soon after they log out, you probably want to keep the record
(to avoid deleting and allocating records many times). If logins are
mostly to unique users, so chances are good you won't see the same user
log back in later, you might as delete each record as it hits zero --
there's little chance you'll get to reuse it, and removing it from the
map reduces total memory usage, and reduces the number of records to
search through.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 11 '06 #6
Right now you're using an O(N*N) algorithm. This is roughly O(N lg
N).
If you remove records as they hit zero, it's O(N lg M), where M is
the
average number of unmatched LOGIN records over given time as you
traverse the file.
I don't quite understant this O(xy) thing. Where can I read about it?
Whether to remove zero records is mostly a question of how often a
specific user is likely to login/out during a run. If they'll login
relatively soon after they log out, you probably want to keep the
record
(to avoid deleting and allocating records many times). If logins are
mostly to unique users, so chances are good you won't see the same
user
log back in later, you might as delete each record as it hits
zero --
there's little chance you'll get to reuse it, and removing it from
the
map reduces total memory usage, and reduces the number of records to
search through.
The hex numbers occour usually only twice - one per login, one per
logout. Each user gets a unique hex for each login (unless he log's
into the same program again)
Anyway:
I've std::sorted the vector with
struct SRT
{
bool operator()(const LINE& l1, const LINE& l2)
{
if(l1.hex==l2.hex)
return l1.time<l2.time;
return l1.hex < l2.hex;
}
};

Then I have to look at each element and it's neighbour only. The
speedup is amazingly! From 10 minutes to 15 seconds now!

Thank you all.
-Gernot

Sep 12 '06 #7
"Gernot Frisch" wrote:
I don't quite understant this O(xy) thing. Where can I read about it?
It's called Big Oh notation. You might start here.

http://en.wikipedia.org/wiki/Big_O_notation
Sep 12 '06 #8
In article <4m************@individual.net>, Me@Privacy.net says...

[ ... ]
I don't quite understant this O(xy) thing. Where can I read about it?
Just about any decent book on algorithms should cover it to at least
some degree. _The Art of Computer Programming_ (~3.3 volumes out of a
planned 7 volumes are complete, at least count) by Donald Knuth is
probably the best known choice. There's also _Introduction to
Algorithms_ by Cormen, Lieserson, Rivest and (in the latest edition) a
fourth guy whose name I can't remember. Knuth is also one of the co-
authors of _Concrete Mathematics_. This isn't exactly a traditional
algorithms book, but it's certainly interesting and useful, and has a
fair amount about computational complexity and big-O notation.

There are quite a few others around as well and a fair number are quite
good, but I feel obliged to caution against anything by Robert
Sedgewick. While I've little doubt that Robert is a very smart guy (he's
invented some nice algorithms), his books pretty much blow. What he
teaches as an algorithm is sometimes almost completely different from
what the rest of the world recognizes that particular algorithm to be.
His exposition of an algorithm is often thoroughly incomplete, and his
explanations so poor they can actually even confuse somebody who already
knows the algorithm in question. I can find nothing about his books to
make them worth recommending at all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 12 '06 #9

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
9
by: Tzu-Chien Chiu | last post by:
Hi, "What methods do you ever use to optimize the programs?" We're developing a graphics chip emulator in C++, but it's very slow for big scenes. Even though this is a cross-platform software,...
5
by: _BNC | last post by:
I've been adapting an older unmanaged C++ legacy app to C#---with limited success. The original app made use of an older, but straightforward, C DLL that worked efficiently under the VC++ 6 model....
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
13
by: Kirk McDonald | last post by:
Say I have a database containing chunks of Python code. I already have a way to easily load, edit, and save them. What is slightly baffling to me is how I can effectively pass this code some...
17
by: Marc | last post by:
Hi, Before I had installed Visual basic.net 2003 on my laptop toshiba Tecra S1 Tecra S1 Centrino 1.6GHz / XP Pro / 15.0 256mb Windows XP servic pack2 After installing Visual Studio 2005...
5
by: howa | last post by:
will performance increae if I removed comments & space from source code using php -w ...? given that i don't need to modify the source code, & don't use any cache?
3
by: Michael | last post by:
I work with a highly programmed Access database (some 15,000 lines of VBA code, much of it automating data entry on forms -- and believe me, it's very tight code). In Access 97, 2000, 2002, and...
13
by: brad | last post by:
Still learning C++. I'm writing some regex using boost. It works great. Only thing is... this code seems slow to me compared to equivelent Perl and Python. I'm sure I'm doing something incorrect....
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.