Re: C++ programming challenge
* Peter Jansson:
On Thu, 11 Jun 2009, Alf P. Steinbach wrote:
It sort of sped up... ;-)
/.../
Cheers & hth.,
- Alf
PS: It's possible to optimize even more but what's the point?
Alf, I have included your modified program as well in the comparison. It
is now on the weblog page.
Uhm, I'm sorry to report that I was lead astray by two Windows "features":
1. In Windows, 'clock', used in that program to measure the time, reports
wall time, not process time.
2. For some obscure reason Windows XP, or perhaps it's the anti-virus, or
whatever, causes a process to run extra slowly the first few times. In
this case I ran the original program twice with a certain medium size
text file, then with modification it ran 400 times faster!, which I could
not believe was an arbitrary effect. But now testing it, and with a much
larger file, it is as you reported, about the same time.
Sincerely,
Peter Jansson
PS: How would you optimize it even more?
On my machine, Windows XP,
#define BUFFER 16*1024U
for the same program text that you have, yields the best times (statistically).
You might want to put that also in your list. :-)
I don't have a bittorrent client so couldn't obtain your file, but with a half
gigabyte text file I constructed from a dictionary text, the processing is about
1/27 of the i/o time. So a marginal improvement, on that order, could be to read
e.g. four bytes (a long) at a time from the buffer to avoid memory accesses.
Just to be thorough I tested using the lower level Posix standard open etc., but
it turned out that it was not significantly faster for this than FILE*.
A less insignificant improvement could be to use a memory mapped file, because
that could conceivably leverage some OS virtual memory optimization. However,
while the Boost library does have some functionality for mapping a file to
memory, it beats me how to use that. It's not exactly well-documented.
Cheers & hth.,
- Alf (victim of Windows (the Eagles' newest greatest hit!))
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!