Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:30:58+00:00 2026-06-06T10:30:58+00:00

After doing the following test: for( i = 0; i < 3000000; i++ )

  • 0

After doing the following test:

for( i = 0; i < 3000000; i++ ) {
    printf( "Test string\n" );
}

for( i = 0; i < 3000000; i++ ) {
    write( STDOUT_FILENO, "Test string\n", strlen( "Test string\n" ) );
}

it turns out that the calls to printf take a grand total of 3 seconds, while the calls to write take a whopping 46 seconds. How, with all the fancy formatting magic that printf does, and the fact that printf itself calls write, is this possible? Is there something that I’m missing?

Any and all thoughts and input are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:31:00+00:00Added an answer on June 6, 2026 at 10:31 am

    How, with … the fact that printf itself calls write, is this possible? Is there something that I’m missing?

    Yes, there is something that you are missing. printf doesn’t necessarily call write every time. Rather, printf buffers its output. That is, it often stores its result in a memory buffer, only calling write when the buffer is full, or on some other conditions.

    write is a fairly expensive call, much more expensive than copying data into printf‘s buffer, so reducing the number of write calls provides a net performance win.

    If your stdout is directed to a terminal device, then printf calls write every time it sees a \n — in your case, every time it is called. If your stdout is directed to a file (or to /dev/null), then printf calls write only when its internal buffer is full.

    Supposing that you are redirecting your output, and that printf‘s internal buffer is 4Kbytes, then the first loop invokes write 3000000 / (4096 / 12) == 8780 times. Your second loop, however, invokes write 3000000 times.

    Beyond the effect of fewer calls to write, is the size of the calls to write. The quantum of storage in a hard drive is a sector — often 512 bytes. To write a smaller amount of data than a sector may involve reading the original data in the sector, modifying it, and writing the result back out. Invoking write with a complete sector, however, may go faster since you don’t have to read in the original data. printf‘s buffer size is chosen to be a multiple of the typical sector size. That way the system can most efficiently write the data to disk.

    I’d expect your first loop to go much faster than the second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that returns a comma-separated string of numbers after doing some
Let's say i have the following string: a test-eh'l I want to capitalize the
As a test, I'm doing the following as the first line in applicationDidFinishLaunching: NSArray
I'm trying to write an Android activity instrumentation test that stops ( onPause() ,
After doing a set , when i try to get a object from memcached,
After doing lot of research in Google, I found this program: #include <stdio.h> int
After doing some Core animations using: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [self setAffineTransform:CGAffineTransformIdentity]; [UIView
After doing a project with WPF and getting very much attached to it's excellent
After doing a subversion merge with a co-worker, my VSeWSS project no longer build
After doing some research on the subject, I've been experimenting a lot with patterns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.