473,769 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help me to optimizing speed

Hi there
Please help me to optimize this code for speed
I added /O2 to compiler settings
I added /Oe to compiler settings for accepting register type request , but
it seems that is not allowed and if I remove register type for "l" , time of
generating codes doesn't change

the original code makes some files , but I removed that section to make it
simple for you to read
please help me to optimize it for faster running
my system is Windows XP , 512 Mb ram , 1.6 Intel
Regards
ArShAm

Code is here :
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

const int NUM=7;//number of characters in each word
const int total=20000000;//total generation
char q[total][NUM+1];// the table

void main(void)
{
int NUMBERS=26;//size of al array
char al[]="abcdefghijklm nopqrstuvwxyz"; //array to generate a random code
register int l;//my windows XP doesn't respect this request

for(int i=1;i<total;i++ )
{
for(int j=0;j<NUM;j++)
{
q[i][j]=al[rand()%NUMBERS];//generating each password
}

for(l=0;l<i;l++ )//comparing if it is unique or not
{
if(!strcmp(q[i],q[l]))
{
printf(" %d was equal to %d with %s:%s value\n",i,l,q[i],q[l]);
i--;
break;
}
}
if(i%10000==0)p rintf("%d\n",i/10000);//each 10,000 times shows that the
program is runing
//printf("%s\n",q[i]);
}
printf("\r\nDON E");
getchar();
}
Jul 19 '05 #1
5 3533
ArShAm wrote:
Hi there
Please help me to optimize this code for speed
OK.
The following information is off-topic here:
<OT> I added /O2 to compiler settings
I added /Oe to compiler settings for accepting register type request , but
it seems that is not allowed and if I remove register type for "l" , time of
generating codes doesn't change

the original code makes some files , but I removed that section to make it
simple for you to read
please help me to optimize it for faster running
my system is Windows XP , 512 Mb ram , 1.6 Intel <OT>
Code is here :
#include "stdio.h" #include <stdio.h>
#include "stdlib.h" #include <stdlib.h>
#include "string.h" #include <string.h>

const int NUM=7;//number of characters in each word
const int total=20000000;//total generation
char q[total][NUM+1];// the table

void main(void) int main(void) // main() returns int...main() returns int...
{
int NUMBERS=26;//size of al array
char al[]="abcdefghijklm nopqrstuvwxyz"; //array to generate a random code
register int l;//my windows XP doesn't respect this request

for(int i=1;i<total;i++ )
{
for(int j=0;j<NUM;j++)
{
q[i][j]=al[rand()%NUMBERS];//generating each password
}

for(l=0;l<i;l++ )//comparing if it is unique or not
{
if(!strcmp(q[i],q[l]))
{
printf(" %d was equal to %d with %s:%s value\n",i,l,q[i],q[l]);
i--;
break;
}
}
if(i%10000==0)p rintf("%d\n",i/10000);//each 10,000 times shows that the
program is runing
//printf("%s\n",q[i]);
}
printf("\r\nDON E");
getchar();
}

Micro-optimization -- or even compiler optimization -- is *not*
going to help you here. You've got an O(n-squared) algorithm where
an O(n log n) algorithm would be more appropriate.

Consider using a std::set.

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #2
"ArShAm" <ar************ @hotmail.com> wrote...
Hi there
Please help me to optimize this code for speed
I added /O2 to compiler settings
I added /Oe to compiler settings for accepting register type request , but
it seems that is not allowed and if I remove register type for "l" , time of generating codes doesn't change

the original code makes some files , but I removed that section to make it
simple for you to read
please help me to optimize it for faster running
my system is Windows XP , 512 Mb ram , 1.6 Intel
Regards
ArShAm

Code is here :
[...]


Instead of comparing each word with all others, sort them and then
run through the sorted array and see if any neighbours are equal.

Victor
Jul 19 '05 #3
In article <bo************ *@ID-89294.news.uni-berlin.de>,
ar************@ hotmail.com says...
Hi there
Please help me to optimize this code for speed
I added /O2 to compiler settings
I added /Oe to compiler settings for accepting register type request , but
it seems that is not allowed and if I remove register type for "l" , time of
generating codes doesn't change
That almost certainly means that the compiler is putting your variable
in a register automatically -- in fact, for most practical purposes, the
register keyword is obsolete.
please help me to optimize it for faster running
As usual, the key to optimizing the code is not microscopic details like
whether a variable ends up in a register, but in improving the
algorithms and/or data structures involved.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
These should really be enclosed in angle brackets instead of quotes
(though changing that is extremely unlikely to change the speed at all
-- and if it does, you're doing other things you really shouldn't (like
creating headers of your own with the same names as those supplied by
the system).
const int NUM=7;//number of characters in each word
const int total=20000000;//total generation
char q[total][NUM+1];// the table

void main(void)
main always returns an int. Again, this won't really affect the speed,
but it's something you should do anyway.
register int l;//my windows XP doesn't respect this request
XP, as such, has nothing to do with it one way or the other -- it's the
compiler, not the OS, that decides what goes into registers. In any
case, you probably have things backwards -- it isn't that it's ignoring
your request to put this in a register. Rather, it's putting it in a
register automatically, whether you ask for it or not.
for(int i=1;i<total;i++ )
{
for(int j=0;j<NUM;j++)
{
q[i][j]=al[rand()%NUMBERS];//generating each password
}

for(l=0;l<i;l++ )//comparing if it is unique or not


Here's where your real problem arises -- verifying uniqueness with a
linear search renders your overall algorithm O(N^2). Keeping the
strings sorted and doing a binary search will reduce this to O(N lg N)
instead -- a massive improvement when you're dealing with 20 million
items (see below for just how massive it really is).

In this case (creating 20 million _small_ strings) it's probably worth
using our own little string-like class instead of the full-blown
std::string, if only to save memory. Using std::map and my own pwd
class, I came up with this:

#include <set>
#include <string>
#include <cstdlib>
#include <iostream>
#include <ctime>

const int total = 20000000;

class pwd {
const static int NUM = 7;
const static int NUMBERS = 26;
char data[NUM];

public:
// generate a random string.
pwd() {
static char letters[] = "abcdefghijklmn opqrstuvwxyz";

for (int i=0; i< NUM; i++)
data[i] = letters[std::rand() % NUMBERS];
}

// std::set requires ability to compare items.
bool operator<(pwd const &other) const {
return -1 == strncmp(data, other.data, NUM);
}

// support writing a pwd to a stream.
friend std::ostream &operator<<(std ::ostream &os, pwd const &p) {
return os.write(p.data , pwd::NUM);
}
};

int main() {
std::set<pwd> passwords;

std::srand(std: :time(NULL));

std::clock_t start = clock();
for (unsigned long size=0; size<total; size++) {
if ( passwords.inser t(pwd()).second )
size++;
if ( size % 10000 == 0)
std::cout << '\r' << size << std::flush;
}

std::clock_t end = clock();

std::cout << "\nTime: " << double(end-start)/CLOCKS_PER_SEC
<< "seconds\n" ;

// show first and last passwords, so the optimizer won't eliminate
// the loop above.
std::cout << *passwords.begi n() << std::endl;
std::cout << *--passwords.end() << std::endl;

#if 0
std::copy(passw ords.begin(),
passwords.end() ,
std::ostream_it erator<pwd>(std ::cout, "\n"));
#endif
return 0;
}

I modified your program slightly, so it would only produce 90 thousand
strings. I compiled that program with MS VC++ 7.1, using:
cl /Oxb2 /G6ry pwds1.cpp
and it ran in 56.4 seconds on my machine. Extrapolating from that, based
on an O(N^2) complexity, I estimate it would take around a month for
your program to produce all 20 million passwords.

Compiled the same way and run on the same machine, the code above
produces 20 million strings in about 58 seconds (i.e. 20 million strings
_almost_ as fast as you were getting 90 thousand).

A micro-optimization (like register) will rarely give an improvement
more than a few percent. If the compiler really screws up and you
manage to enregister something inside of a really tight loop, you might,
concievably get an improvement of, say, 50:1, but that's _extremely_
rare (I don't think I've ever seen it). By contrast, this algorithmic
improvement gave an improvement of around fifty _thousand_ to 1, with
only minimal investment... :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #4
Thanks Dear Jerry,
It was gr8
before I recieve your answer I tried to change my generation code , and I
think that will be much better
but there is a problem , and that is the program crashes with a huge number
for total

here is the code:

#include "stdio.h"
#include "time.h"
#include "stdlib.h"
#include "string.h"
#include "myqueue.h"
#include <sys/timeb.h>

const int NUM=10;
char element[NUM];
int a[NUM]={0};
char al[]="ABCDEFGHIJKLM NOPQRSTUVWXYZ";
int size;

void main(void)
{
const int total=1000000;
size=sizeof(al)-2;
void gen(void);

myQueue* table[total];

for(int r=0;r<NUM;r++)
element[r]='A';
for(int l=0;l<total;l++ )
{
gen();
table[l]=new myQueue();
strcpy(table[l]->element,elemen t);
}

srand( (unsigned)time( NULL ) );
int tot=total;

FILE *file;
file=fopen("rep ort.pin","w");

int Sorted[total],shuffled[total];
int i3, j3;

for ( i3 = 0; i3 < total; i3++ ) Sorted[i3] = i3;

for ( i3 = 0; i3 < total; i3++ )
{
j3 = rand() % (total-i3);
shuffled[i3] = Sorted[j3];

Sorted[j3] = Sorted [ total-1-i3 ];
}

for(register int i=0;i<total;i++ )
{
fprintf(file,"% 06d:%s\n",i,tab le[shuffled[i]]->element);
}
fclose(file);

printf("Done\n" );
exit(0);
}

void gen(void)
{
a[0]++;
for(int k=0;k<NUM;k++)
{
if(a[k]>size)
{
a[k]=0;
a[k+1]++;
}
}

for(int y=0;y<NUM;y++)
{
element[y]=al[a[y]];
}
}

//and the myQueue class :
const int NUM1=10;
class myQueue
{
public:
char element[NUM1];
myQueue();
virtual ~myQueue();

myQueue(){
strcpy(element, "");}
~myQueue(){}

};
Thank you for your help
Regards
ArShAm
Jul 19 '05 #5
ArShAm wrote:
Thanks Dear Jerry,
It was gr8
before I recieve your answer I tried to change my generation code , and I
think that will be much better
but there is a problem , and that is the program crashes with a huge number
for total

here is the code:

#include "stdio.h"
#include "time.h"
#include "stdlib.h"
#include "string.h"
#include "myqueue.h"
#include <sys/timeb.h>
I really don't understand.
Why are the standard header files using '"'
excepth the last one? Be consistent and use
angle brackets '<' and '>' instead of quotes (").

const int NUM=10;
char element[NUM];
int a[NUM]={0};
char al[]="ABCDEFGHIJKLM NOPQRSTUVWXYZ";
int size;

void main(void)
Hmmm. How many people have told you that main()
returns int?
Make the change.
{
const int total=1000000;
size=sizeof(al)-2; Why the "-2"?
I could understand a "-1" because of the terminating
nul character, but why "-2". some comments would
really help.

void gen(void); Better naming would help too.
What does this function do?

myQueue* table[total]; The declaration for this class should be before
its usage.
Why do you need 10,000,000 pointers to queues?
So far, this is a memory hog program. See below.

for(int r=0;r<NUM;r++)
element[r]='A'; Prefer library routines:
memset(element, 'A', sizeof(element) );
or
std::fill(eleme nt, element + num, 'A');
they may be optimized for your processor, where
a simple "for" loop isn't.

for(int l=0;l<total;l++ )
{
gen();
table[l]=new myQueue();
strcpy(table[l]->element,elemen t); In order for strcpy to work, there must be a
terminating nul ('\0') character within the
string.
I didn't see any terminating null placed in the
"element" array. The strcpy() function is probably
the cause of your crash. It will copy until it
_finds_ a nul character or it accesses undefined or
protected memory.
I think you want:
memcpy(table[l]->element, element, sizeof(element) );
or
std::copy(eleme nt, element + NUM, table[i]->element);
}
At this point, you have allocated:
10,000,000 pointers to myQueue objects
10,000,000 myQueue objects.
Insert these statements into your code:
cout << "current memory allocation: "
<< total * (sizeof (myQueue *) + sizeof(myQueue) )
<< "\n";
srand( (unsigned)time( NULL ) ); Search the C FAQ and the C++ FAQ and the C language
newsgroup (news:comp.lang .c) about random numbers.
You'll find some interesting information.

int tot=total;

FILE *file;
file=fopen("rep ort.pin","w"); Are you programming in C or C++?
ostream file("report.pi n");


int Sorted[total],shuffled[total]; Memory Hog!
At this point, you have asked the compiler to allocate
20,000,000 integers in the "automatic" area (a.k.a stack).

Add these lines to your code:
cout << "Automatic variable allocation: "
<< 2 * total * sizeof(int)
<< "\n";

int i3, j3;

for ( i3 = 0; i3 < total; i3++ ) Sorted[i3] = i3;

for ( i3 = 0; i3 < total; i3++ )
{
j3 = rand() % (total-i3);
shuffled[i3] = Sorted[j3];

Sorted[j3] = Sorted [ total-1-i3 ];
} I believe you would do better to use the shuffle algorithm
of the STL.

for(register int i=0;i<total;i++ )
{
fprintf(file,"% 06d:%s\n",i,tab le[shuffled[i]]->element);
} Here is a big waste of time. Don't bother with register
variables. Formatted output requires a lot of time, so
making the index variable as a register will not save any
significant time.
fclose(file);

printf("Done\n" );
exit(0);
}

void gen(void)
{
a[0]++;
for(int k=0;k<NUM;k++)
{
if(a[k]>size)
{
a[k]=0;
a[k+1]++;
}
}

for(int y=0;y<NUM;y++)
{
element[y]=al[a[y]];
}
} The above should not modify global variables. This poses
a hindrance when reading the code. Pass pointers to the
variables that will be modified. Also, use comments to
explain what this function is doing.

//and the myQueue class :
const int NUM1=10;
class myQueue
{
public:
char element[NUM1];
myQueue();
virtual ~myQueue();

myQueue(){
strcpy(element, "");}
~myQueue(){}

}; What is the purpose of this class?
It doesn't look like a queue container.

Thank you for your help
Regards
ArShAm


You could gain a lot of speed by replacing the "myQueue"
class with a fixed size vector or array. The class
provides no useful functionality, so remove it.

Also, why do you need to store 10,000,000 random strings
of 10 characters in length to a file. Your file will be
a minimum of 10,000,000 * 10 bytes long or 100MB.

If you're looking to write a program that generates
possible passwords, a more successful approach is to
use common names, then common words. After those fail,
then generate the random list.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #6

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

Similar topics

6
2876
by: A Future Computer Scientist | last post by:
A question: Is it really important to think about optimizing the native code or optimizing it for P Code? Or does the code you write make a difference?
13
1882
by: Nickolay Kolev | last post by:
Hi all, I am currently writing some simple functions in the process of learning Python. I have a task where the program has to read in a text file and display some statistics about the tokens in that file. The text I have been feeding it is Dickens' David Copperfield. It is really simple - it reads the file in memory, splits it on whitespace, strips punctuation characters and transforms all remaining
2
1930
by: m3ckon | last post by:
Hi there, had to rush some sql and am now going back to it due to a slow db performance. I have a db for sales leads and have created 3 views based on the data I need to produce. However one o the views, which has subqueries to the other views is VERY slow and it needs to be speeded up, but am unsure how, can anyone help... below is the sql?
7
728
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked with binary files before so I'm confused with how to store the binary file into memory. What sort of...
4
2534
by: J. Campbell | last post by:
From reading this forum, it is my understanding that C++ doesn't require the compiler to keep code that does not manifest itself in any way to the user. For example, in the following: { for(int i = 0; i < 10; ++i){ std::cout << i << std::endl; for(int j = 0; j < 0x7fffffff; ++j){} } }
3
3333
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any resources that explains how to optimize code i.e. give some rules on c++ optimization? e.g. using memcpy to copy an array (which i have done). Also, what is the best sorting algorithm out there for sorting an array of of size 100 or less? I have...
31
2644
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant but it appears to work. using time, i measured it takes .041s to execute, which i admit isnt much. but, the problem is that this procedure will be called many, many times in my project (probably at least a few thousand times, if not more) so...
24
3166
by: Richard G. Riley | last post by:
Without resorting to asm chunks I'm working on a few small routines which manipulate bitmasks. I'm looking for any guidance on writing C in a manner which tilts the compilers hand in, if possible, a compiler/underlying processor independant way : althought to be fair I cant see this stuff on anything other than x86, but who knows. I found some ok info here: http://www.eventhelix.com/RealtimeMantra/Basics/OptimizingCAndCPPCode.htm...
4
11961
by: RS | last post by:
Hi all, I was told that using unsigned int instead of int can speed up the code. Is this true? If so, why? Are there are any other rules one should follow to optimize the code for speed (i.e. using float instead of double, short instead of int, unsigned short instead of short, etc.)? Thanks,
0
9579
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
9422
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,...
1
9987
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
8867
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...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
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...
1
3952
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
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2812
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.