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

Will this code be faster using stdio???

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
1 1463
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Bachmann | last post by:
Two years I wrote a simple cesar encryption program, it worked but it relied on ASCII. So today I tried to make an portable cesar encryption. Please tell me if the code below is really protable. ...
7
by: Sunil Varma | last post by:
Is accessing function by it's name faster or accessing it by its address faster?
11
by: Eigenvector | last post by:
I apologize if this is a trivial question, but it's always made me wonder when I have to compile my code. There are some #includes that you don't really need to reference in your library and...
5
by: sololoquist | last post by:
#define COUNT_UP #include <stdio.h> #define N 10 int main() { int i; #ifdef COUNT_UP for (i = 0; i < N; i++)
2
by: Alexandre Ferrieux | last post by:
Hi, In a recent thread I discovered why the "for line in f" idiom was not suitable for live sources (pipes, sockets, tty). The reason is that it uses buffering on input, blocking on a full...
25
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not...
33
by: llothar | last post by:
I'm afraid that the GIL is killing the usefullness of python for some types of applications now where 4,8 oder 64 threads on a chip are here or comming soon. What is the status about that for...
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
48
by: istillshine | last post by:
When I used gprof to see which function consumed most running time, I identified the following one. sz was less than 5000 on average, but foo had been called about 1,000,000 times. I have tried...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.