Thread: cout == slow (prime numbers)

  1. #1
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    cout == slow (prime numbers)

    i've been working on a prime number program on and off since last year and i thought everyone (who doesn't know this already) how slow cout really is. for example when running my program and couting all the primes between 1 and 10 million my program completed in 125.5 seconds whereas w/ cout commented out, the program ran in 17.7 seconds!!!

    also, if any1 knows how fast direct access files are (in a vague comparision to whatever you can think of...) it would be nice. i am planning on implementing them to hold the found prime numbers.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Store the whole lot in one big char array, and fwrite() the whole array in one go at the end
    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.

  3. #3
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    LOL

    that char would have to be... *counts fingers* very very large. when i ran it to 25 million earlier today i found (well i can't remember exactly) i think 1.7 million primes... each of those is anywhere from 1 to 7 digits... that's means i would have to have something like this.

    Code:
    char primes[7000000];
    which seems pretty ridiculous... not to mention using that much ram will slow down the program and i could overflow the variable and lose all the primes i just found
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Does the 17.7 sec run include writing them to a file? If no, try ofstream. If yes:

    Is this what you're trying to make faster? If so, you could try fopen(), fprintf().

    And if you don't care about portability, for windows there are CreateFile(), WriteFile(), etc. which are supposed to be fast.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    File access or console write access is slow no matter what method you use. Yes, cout /ofstream are slower than raw solutions like fputs or WriteFile, but only marginally so, the real speed problem is the actual access, especially with files.
    Not using endl for example should speed up the streams quite a bit.

    For the char array: 7 MB aren't ridiculous at all. Any modern game uses far more than that, and prime number seekers are memory intensive, nobody will complain. But you should allocate the thing via new, not on the stack. SGI's rope class might be a good choice too (I believe STLport contains it too).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you don't have to store all 7MB at once (but you know already how much space it will take from the file you've already written right)

    Besides, how much space do all your primes take up, excluding the textual representation of them?

    Create a buffer of whatever size you feel happy with (or just experiment). When the buffer is full, you write it out and start over.

    > i could overflow the variable and lose all the primes i just found
    Only if your code was so hopelessly bug ridden as to write over memory it didn't own.
    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.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    195
    wait, so if cout is slow, what is the fast solution that reported the quick 17.7 seconds?

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ no output?

    what about writing them into an array of unsigned long ints? or a two dimensional array?
    Code:
    char array[numberofprimes][longestprimelength];
    finding prime numbers is a pretty heavy operation... there's no way around it... either eat memory, slow down with constant outputs, a mix of both, or no ouput at all...
    Last edited by major_small; 12-17-2003 at 11:10 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    The *print* functions are actually faster I believe. I rember I tested this a while ago. Though of course, both cout and the *print* thingys are slower than no output at all.

    Why don't you just store the primes in an array of some bin-type, maybe int64_t. I doubt you could produce enough primes to use up your memory

    Does you program find primes based on probability or does it actually find all primes ?
    [code]

    your code here....

    [/code]

  10. #10
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    thanks for the responses guys

    at the moment there is no output because of the huge speed difference... (well aside from the gettime output at the end)

    the program actually finds all the primes and doesn't just guess which numbers are primes

    so are creatfile() and writefile() faster than direct access?

    basically at this point im hoping to make a program that runs fast (albeit slower than at the moment) that uses files so i have some sort of hardcopy of the primes found.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Actually, in an optimized implementation, cout is way faster than printf.
    cout does the type-checking at compile-time. printf on the other hand parses a format string at run-time. This is *much* slower.

    The reason cout is slower on some implementations of C++ is because the programmers have been lazy (cout uses printf internally of something like that).
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could try write() from ofstream. I just ran a program which wrote 10,000,000 ints to a file using write(), and it took 5-7 seconds. This would be considerably more ints than the primes between 0 and 10,000,000.

    Here's what I used to write one int:
    Code:
    out.write(reinterpret_cast<char *> (&num), sizeof(num));
    One would guess write() from ifstream or the c-style fwrite() would be comparable in speed.

    If this isn't fast enough, then try writing a block at a time, as Salem suggested.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I just used fwrite() in combination with a buffer to write 10,000,000 ints, and it took 2 seconds. Without a buffer, it's about the same speed as ofstream's write().

    So with a buffer, fwrite() appears faster than ofstream's write(). Without a buffer, they are about the same speed.

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I would use a buffer... if you have the memory availible, use as much of it as you can/need (unless there's a reason you don't want), and then dump the buffer into a file and maybe output it to the screen at the end of the run...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    hmmmmm

    im afraid what you guys are talking about is over my head at the moment. i only know the fstream header file ofstream and direct access (also inhe fstream header file)...

    (note: i ran the program with my school comp left on to find all the primes between 1 and 1 billion (10 digits) last night and it took 4 hours 40ish minutes to complete... meaning im nowhere near factoring a 200 digit number for the http://www.rsasecurity.com/rsalabs/c...ges/index.html contest lol)
    Last edited by heat511; 12-19-2003 at 02:40 PM.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-lvalue error
    By zlb12 in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 10:43 AM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM