Will this code be faster using stdio???

S

samoukos

Hello i had to do this project but at school they tell me that it will
be faster using stdio insteed of fstream... Is that right??? if it is
faster can anyone suggest how this code will be using stdio???thank
you????

here is the code:

//Made by Samuel Johnson
//email:[email protected]
#include<iostream>
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::eek:fstream;
using std::endl;
using std::ios;

int main ()
// Lawdy, Lawdy I do declare!
{
ifstream fin;
ofstream fout;
int i,j,max,imax;
char rod;
int sum[255];
fin.open("friktories.in");
fout.open("friktories.out");

//----------------------------------
// Initializes array for summing
// ASCII (extended) characters
for (i=0;i<255;i++)
{
sum=0;
}
//----------------------------------
// Reads file one char @ a time
// into char var rod, then sums it
// in array sum with index rod
//
// can this be faster using stdio?
//
while (!fin.eof())
{
rod=fin.get();
sum[rod]=sum[rod]+1;
}
//----------------------------------
// Finds max frequency in sum array,
// writes it to output file,
// zeros the value in sum array,
// and repeats for all ASCII char
// that we are interested in. Includes
// ASCII 32 (space)
//
//
max=-1;
for (j=0;j<27;j++)
{
for (i=97;i<124;i++)
{

if (sum>max)
{
max=sum;
imax=i;
}
}

if (sum[32]>max)
{
max=sum[32];
imax=32;
}
fout << char(imax)<< " "<<sum[imax]<<endl;
sum[imax]=-1;
max=-1;
}
//----------------------------------
// Closes the files nicely!
fin.close();
fout.close();
fout << endl;
return 0;
}
 
M

michael.goossens

Implement both approaches, run them a zillion times in main and record
the time each method took. A tip I got from a smart guy :p. (if the
times are near eachother then don't conclude anything since you are
working on a multiprocess os ;).

xxx Michael
 
H

Hans Mull

samoukos said:
Hello i had to do this project but at school they tell me that it will
be faster using stdio insteed of fstream... Is that right??? if it is
faster can anyone suggest how this code will be using stdio???thank
you????

here is the code:

//Made by Samuel Johnson
//email:[email protected]
#include<iostream>
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::eek:fstream;
using std::endl;
using std::ios;

int main ()
// Lawdy, Lawdy I do declare!
{
ifstream fin;
ofstream fout;
int i,j,max,imax;
char rod;
int sum[255];
fin.open("friktories.in");
fout.open("friktories.out");

//----------------------------------
// Initializes array for summing
// ASCII (extended) characters
for (i=0;i<255;i++)
{
sum=0;
}
//----------------------------------
// Reads file one char @ a time
// into char var rod, then sums it
// in array sum with index rod
//
// can this be faster using stdio?
//
while (!fin.eof())
{
rod=fin.get();
sum[rod]=sum[rod]+1;
}
//----------------------------------
// Finds max frequency in sum array,
// writes it to output file,
// zeros the value in sum array,
// and repeats for all ASCII char
// that we are interested in. Includes
// ASCII 32 (space)
//
//
max=-1;
for (j=0;j<27;j++)
{
for (i=97;i<124;i++)
{

if (sum>max)
{
max=sum;
imax=i;
}
}

if (sum[32]>max)
{
max=sum[32];
imax=32;
}
fout << char(imax)<< " "<<sum[imax]<<endl;
sum[imax]=-1;
max=-1;
}
//----------------------------------
// Closes the files nicely!
fin.close();
fout.close();
fout << endl;
return 0;
}

IF you think about optimizing, read Agner Fog's optimizing C++
(http://agner.org/optimize/optimizing_cpp.pdf)

Kind regards
 
D

dizzy

Hans said:
IF you think about optimizing, read Agner Fog's optimizing C++
(http://agner.org/optimize/optimizing_cpp.pdf)

"6.18 Runtime type identification (RTTI)
Runtime type identification adds extra information to all class objects and
is not efficient. If
the compiler has an option for RTTI then turn it off and use alternative
implementations."

I would say "LOL" but this is not IRC (it doesn't say if it is less
efficient than something that does the same thing, it doesn't say if one
should disable RTTI only when not using and when paying for it's price is
too much, etc it just makes generic statements like that). It seems to me
to be more addressed to people that already know how to optimize something
and as such put all those generic statements in the proper context.

A better reference IMO would be the C++ performance technical report:
http://www.open-std.org/JTC1/SC22/WG21/docs/TR18015.pdf
 
J

Jerry Coffin

Hello i had to do this project but at school they tell me that it will
be faster using stdio insteed of fstream... Is that right??? if it is
faster can anyone suggest how this code will be using stdio???thank
you????

It's impossible to say in any general way -- it could be faster or it
could be slower. It's unlikely, but in theory it could depend on the
phase of the moon and whether it's cloudy today...

OTOH, I do have some comments about how the code is written. They're
more likely to affect maintainability than speed though...

[ ... ]
ifstream fin;
ofstream fout;
int i,j,max,imax;
char rod;
int sum[255];
fin.open("friktories.in");
fout.open("friktories.out");

In general, you're better off initializing variables than creating them
and later assigning values to them. For a few examples:

int sum[255] = {0};
ifstream fin("friktories.in");
istream fout("friktories.out");

In reality, I'd advise against using '255' here either -- I'd use
UCHAR_MAX or std::numeric_limits said:
//----------------------------------
// Initializes array for summing
// ASCII (extended) characters
for (i=0;i<255;i++)
{
sum=0;
}


With the initialization above, this is unnecessary. If it was necessary,
std::fill_n might be a better way to do the job.
//----------------------------------
// Reads file one char @ a time
// into char var rod, then sums it
// in array sum with index rod
//
// can this be faster using stdio?
//
while (!fin.eof())
{
rod=fin.get();
sum[rod]=sum[rod]+1;
}

The biggest problem here is that the code doesn't work correctly. eof()
only becomes true when you attempt to read data after you've already
reached the end of the file. The effect is that you'll count the last
character twice.
//----------------------------------
// Finds max frequency in sum array,
// writes it to output file,
// zeros the value in sum array,
// and repeats for all ASCII char
// that we are interested in. Includes
// ASCII 32 (space)
//
//
max=-1;
for (j=0;j<27;j++)
{
for (i=97;i<124;i++)
{

if (sum>max)
{
max=sum;
imax=i;
}
}

if (sum[32]>max)
{
max=sum[32];
imax=32;
}
fout << char(imax)<< " "<<sum[imax]<<endl;
sum[imax]=-1;
max=-1;
}


This is pretty ugly. First of all, it assumes ASCII encoding, which is
more or less a rarity anymore (most computers have used something like
ISO 8859-x for quite a while now). Second, all the ranges are given
numerically, even though they refer to characters, making it hard to
figure out what it's doing. Third, it's pretty inefficient -- your loops
execute 27*(124-97) times. Fourth, it seems to be basically a collection
of special cases instead of giving some sort of general rule for what it
cares about and what it doesn't. Finally, it's not at all apparent what
the outer loop (using 'j' as its index) is supposed to accomplish.

From the looks of things, you basically care about lower-case letters
plus space and (possibly) a couple of punctuation characters -- though
it looks entirely possible that those were included only by accident.

If you really don't care about characters other than letters and space,
I'd ignore the other characters while you're counting -- which should
save some space and a bit of difficulty as well. I'd look up the
contents of <ctype.h> or <cctype> (they're nearly equivalent) to get
some help on classifying characters. Instead of repeatedly searching for
the largest item while printing things out, I'd consider sorting the
counts, then printing them out in sorted order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top