By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

Will this code be faster using stdio???

P: n/a
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:sa*******@gmail.com
#include<iostream>
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::ofstream;
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[i]=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[i]>max)
{
max=sum[i];
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;
}
Jan 29 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
samoukos schrieb:
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:sa*******@gmail.com
#include<iostream>
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::ofstream;
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[i]=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[i]>max)
{
max=sum[i];
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
Jan 29 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.