473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#include<cstdli b>
#include<fstrea m>
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("frikt ories.in");
fout.open("frik tories.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 1476
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<iostre am>
#include<cstdli b>
#include<fstrea m>
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("frikt ories.in");
fout.open("frik tories.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
2065
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. Thanks in advance. #include <stdlib.h> #include <stdio.h> #include <string.h>
7
1522
by: Sunil Varma | last post by:
Is accessing function by it's name faster or accessing it by its address faster?
11
1934
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 header references in the compilation string. For instance the standard hello.c #include <stdio.h> int main()
5
3539
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
2981
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 buffer read before anything. When I asked why it did it this way, the answer came up that it made it faster. Now, *why* is such buffering gaining speed over stdio's fgets(), which
25
3782
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 let me down, but it has proved surprisingly slow on one puzzle. The puzzle is: p is the perimeter of a right angle triangle with integral length sides, {a,b,c}. which value of p < 1000, is the number of solutions {a,b,c} maximised? Here's my...
33
3703
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 the future of python? I know that at the moment allmost nobody in the scripting world has solved this problem, but it bites and it bites hard. Only groovy as a Java Plugin has support but i never tried it. Writing an interpreter that does MT this...
23
2511
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) return s def descrambleLine(line):
48
2102
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 using "register sum = 0.0" and saw some improvement. My question is how to improve foo further to make it faster. double foo(double *a, double *b, int sz) { double sum = 0.0;
0
8356
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
8551
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
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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
1775
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.