Weird result with code

This is a discussion on Weird result with code within the Tech Board forums, part of the Community Boards category; Hello, I'm totally new to programming. I have a issue with hard disk performance i/o with my windows 2003 servers ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Weird result with code

    Hello, I'm totally new to programming.
    I have a issue with hard disk performance i/o with my windows 2003 servers
    one of my vendor sent me the code below, they said it's testing write speed of hard disk but when I run this code on few of my windows 2003 servers, the actual file size are different.
    about 60% of them, it creates file size with 55MB but on rest 40% of servers I have, it creates file size of 5 MB. weird thing is that one that generates 5MB text file, it takes me about 2 minutes in order to finish generating file. but when it generates 55MB file, it takes less than 10 seconds.

    all of servers are running windows 2003 standard version on a RAID 1 confuguration. I checked if any of windows updates, windows services, or drivers are causing this issue but I can't find any so that I'm hoping if anyone can give me some advice for what's causing different result.

    Here's the code

    Code:
    for (int i = 0; i < 100000 ; i++) { FILE *pFile = fopen( "text.txt" , "at"); 
    fprintf( pFile, "1234567890feoiv jeoifjonfrjobejrgojbomerjgobogorgmborjbo\n" ); 
    fclose(pFile); } 
    
    FILE *pFile = fopen( "text.txt" , "at"); 
    fprintf( pFile, "VERSION:1:TIME:%d\n" , timeGetTime() - dwPre ); 
    fclose( pFile );
    Thank you
    Last edited by Salem; 12-13-2010 at 03:46 PM. Reason: Half-assed job at reformatting

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First, who taught you to write code on 1 line like that?

    Code:
    for (int i = 0; i < 100000 ; i++) 
       { 
          FILE *pFile = fopen( "text.txt" , "at"); 
          fprintf( pFile, "1234567890feoiv jeoifjonfrjobejrgojbomerjgobogorgmborjbo\n" ); 
          fclose(pFile); 
       } 
    
       FILE *pFile = fopen( "text.txt" , "at"); 
       fprintf( pFile, "VERSION:1:TIME:%d\n" , timeGetTime() - dwPre ); 
       fclose( pFile );
    First of all I'm surprised it compiles. My C documentation does not show an "at" mode for file io at all. That's not to say it doesn't exixt, but it's not part of standard C.

    Secondly it's not much of a benchmark... there is no intial time marker, only one at the end. Then there's the matter that it's going to be entirely dependent on a huge number of variables such as disk buffering, file fragmentation, other processes running at the time and of course the always present variability in Windows time sharing.

    Why does the time for this vary so much? Quite probably because the server showing you the painfully slow results is also the busiest of the bunch...

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Hello there,
    First of all I'm not the creator of this code and I have totally new to programming.
    this code is given by one of my vendor and they told me to use this code for checking disk write speed because my server is designed to generate a log file and write any events on the log file.

    problem is I have no idea if there's anything wrong with this code.
    I only assume that this code will create text.txt file and writes text "1234567890feoiv jeoifjonfrjobejrgojbomerjgobogorgmborjbo" 100000 times, writes time it took and then close file. Am I right?

    the purpose of testing is to check writing text speed of the server and as far as I understand this code is taking that action.

    if you anyone can provide appropriate code for checking disk write speed or
    fix any unnessasory code or add need code would be very much appreciated.

    Thank you

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,760
    The "t" is a microsoft extension.
    fopen, _wfopen (CRT)

    > about 60% of them, it creates file size with 55MB but on rest 40% of servers I have, it creates file size of 5 MB
    The test file is never deleted, it just grows each time you run the test.
    For sure, it will grow by about 5MB each time you run the test.

    From a disk exercise point of view, all those open/close are a complete waste of time on things which have nothing to do with disk access.

    Not all hard disks are equal.
    Hard disks | Reviews | PC Pro
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Quote Originally Posted by raesoo80 View Post
    I only assume that this code will create text.txt file and writes text "1234567890feoiv jeoifjonfrjobejrgojbomerjgobogorgmborjbo" 100000 times, writes time it took and then close file. Am I right?
    Nope. Both open and close stream operations are inside the loop. These will also be repeated one hundred thousand times.

    the purpose of testing is to check writing text speed of the server and as far as I understand this code is taking that action.
    Nope. It's also including opening and closing operations and it's not recording the moment the test has started.

    if you anyone can provide appropriate code for checking disk write speed or fix any unnessasory code or add need code would be very much appreciated
    See DISKSPEED - hard disk speed test. Not as simple as you would expect, is it?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mario F. View Post
    Nope. Both open and close stream operations are inside the loop. These will also be repeated one hundred thousand times.
    AKA... "How I thrashed my hard disk to death"... since the r/w head has to traverse to the directory then back to the file's lead sector each time.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Quote Originally Posted by Salem View Post
    The "t" is a microsoft extension.
    fopen, _wfopen (CRT)
    No, it's not.
    Quote Originally Posted by http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
    With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
    Additional characters may follow the sequence, although they should have no effect. For example, "t" is sometimes appended to make explicit the file is a text file.
    In the case of text files, depending on the environment where the application runs, some special character conversion may occur in input/output operations to adapt them to a system-specific text file format. In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability.
    For the modes where both read and writing (or appending) are allowed (those which include a "+" sign), the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a reading operation followed by a writing operation or a writing operation followed by a reading operation.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Quote Originally Posted by User Name: View Post
    No, it's not.
    Yes, it is. Read the text you quoted more carefully.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,760
    The cplusplus reference material is buggy to say the least.
    Just look up scanf and see how much is missing.

    http://www.open-std.org/jtc1/sc22/wg...69/n869.pdf.gz
    Quote the actual standard.
    Page 285 - there is no "t" mode.
    Quote Originally Posted by draft c99
    Description
    2 The fopen function opens the file whose name is the string pointed to by filename,
    and associates a stream with it.
    3 The argument mode points to a string. If the string is one of the following, the file is
    open in the indicated mode. Otherwise, the behavior is undefined.214)
    r
    open text file for reading
    w
    truncate to zero length or create text file for writing
    a
    append; open or create text file for writing at end-of-file
    rb
    open binary file for reading
    wb
    truncate to zero length or create binary file for writing
    ab
    append; open or create binary file for writing at end-of-file
    r+
    open text file for update (reading and writing)
    w+
    truncate to zero length or create text file for update
    a+
    append; open or create text file for update, writing at end-of-file
    r+b or rb+ open binary file for update (reading and writing)
    w+b or wb+ truncate to zero length or create binary file for update
    a+b or ab+ append; open or create binary file for update, writing at end-of-file
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 10:00 PM
  2. weird problem with code, not displaying what it should
    By chris285 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 11:04 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21