Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Will this code be faster using stdio???
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jerry Coffin, post: 3423828"] 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... [ ... ] 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 [i] With the initialization above, this is unnecessary. If it was necessary, std::fill_n might be a better way to do the job. 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. [i][i] 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.[/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Will this code be faster using stdio???
Top