Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Tor Rustad <tor_rustad@xxxxxxxxxxx>
- Date: Fri, 14 Dec 2007 23:08:43 +0100
Stephen Sprunk wrote:
"Tor Rustad" <tor_rustad@xxxxxxxxxxx> wrote in message news:8uydnTeonY9bVv_a4p2dnAA@xxxxxxxxxxxxxxStephen Sprunk wrote:"Tor Rustad" <tor_rustad@xxxxxxxxxxx> wrote in message news:o8mdnaZ89OVSVvza4p2dnAA@xxxxxxxxxxxxxxStandard C IO no buffering...
Buffering turned off
Written 1221 pages of ca. size 65536 (80019456 bytes)
CPU time 0.26
DiskIO 300.55 Mb/s
300.55 Mb/s is too good, the theoretical peek should
have been 150 Mb/s, I don't know why Linux is this
blitzing fast! Does it have anything to do with dual
core CPU??? *strange*
The speed you're measuring is how fast the fwrite() call returns, not the disk write performance. Most likely, fwrite() returns after putting the data into a buffer but before the hardware actually acknowledges it's been written to disk. Modern OSes do all sorts of things like that to improve performance.
Nope, the clock was started *before* fopen(), and stopped *after* fclose().
fclose() just closes the FILE* (and related fd); it does _not_ guarantee that the data is actually physically on the disk. There may be some OS-specific function that will give you the indication you're wrongly assuming you're getting, but it's not on by default.
If Linux is using lazy commit, i.e. allowing a file to be e.g. closed, before flushing system IO buffers, that would be highly interesting/bad, particularly for those working with critical data.
It's completely normal. That's why one should always shut down machines cleanly instead of pulling the plug -- and why data (and filesystems) tend to get corrupted when the power goes out. Even the disks lie to the OS about when data is written; as soon as the data is in the drive's cache, it tells the OS it's done writing so that the OS can reuse the buffer(s). Only top-of-the-line controllers with battery-backed caches are immune from these sorts of problems (and even then, you have to boot the machine again and let it finish writing before removing the disk from the system).
Some years ago, I was told that lazy commit, was the reason we didn't run *any* production systems on Linux, now it's allowed in *some* cases to use this OS. If an OS don't flush system buffers when a file is closed, or is doing a fflush() in an async manner, then it is impossible to code a bullet-proof C program with file I/O, in that environment.
The C standard is silent here, data has only to be transfered to the host environment, depending on QoI, this data may written to persistent storage, before fclose() return, or program exit().
Just consider this, a DB program start a transaction with "begin work", then perform lots of updates, before "commit work"... if you can't trust that the data has been saved on disk, then you risk that the transaction can be lost, in case of e.g. power outage or HW failures (e.g. disk controller failure).
If a disk controller lie, the flaw is limited to that unit. If an OS lie, that is an important thing to know about, and I don't think I would like to use such an OS, for work related production systems.
Added another test case, this time using low-level POSIX IO
functions and sync'ing the file *before* close'ing it, the result was sky
high IO, 434 Mb/s! So, the IO subsystem file writes is more than 10
times faster on Linux, than under Windows XP, tests was done with
identical HW and C source.
*amazing*
It's not so amazing when you realize you're measuring the speed of the OS's I/O system, not the hardware's.
Well, in the benchmark I ran, the result couldn't be explained by disk cache alone, since this is somewhere in the range of 8 Mb - 32 Mb, while the measured IO peek, was >150 Mb/s too high.
Also, since I did an explicit call to fsync() in the last test-case, the data should have been committed to disk, before the stop timer was set. A clear sign that something was wrong, was that fsync()'ing, boosted the performance by another 100 Mb/s!
I think the simple answer here, is that the clock() implementation on Linux, measured processor time used in *user* space only, ignoring the (significant) time spent in system calls. :)
--
Tor <bwzcab@xxxxxxxxx | tr i-za-h a-z>
.
- Follow-Ups:
- References:
- Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: c
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Tor Rustad
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Keith Thompson
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Tor Rustad
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Keith Thompson
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Tor Rustad
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Stephen Sprunk
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Tor Rustad
- Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- From: Stephen Sprunk
- Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- Prev by Date: Re: I want o use the function alternate to a MACRO
- Next by Date: Re: smuggling data in and out of alarm handlers and the like
- Previous by thread: Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- Next by thread: Re: Help me, a friend of mine wrote a program in C# and I wrote the same program in C..His is faster than mine on the same machine...How Come ?
- Index(es):
Relevant Pages
|