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

  • SEARCH
  • Home
  • 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 8914211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:39:45+00:00 2026-06-15T04:39:45+00:00

I am redirecting some output to a file in three different ways and each

  • 0

I am redirecting some output to a file in three different ways and each of these take clearly different amount of time.

$ >/tmp/file ; time for i in {1..1000}; do for j in {1..1000}; do echo $i $j >> /tmp/file; done; done

real    0m33.467s
user    0m21.170s
sys     0m11.919s

$ >/tmp/file ; exec 3>/tmp/file; time for i in {1..1000}; do for j in {1..1000}; do echo $i $j >&3; done; done; exec 3>&-

real    0m24.211s
user    0m17.181s
sys     0m7.002s

$ >/tmp/file ; time for i in {1..1000}; do for j in {1..1000}; do echo $i $j; done; done >> /tmp/file 

real    0m17.038s
user    0m13.072s
sys     0m3.945s

Can someone explain the differences here. My current understanding/doubts are:

  1. 1st is slowest as it opens/closes the file multiple times while others only do it only once. Is that right? What about buffering. Normally, I would expect all output to get buffered in which case we should not have such large time differences.
  2. In 3rd, if all the output is only written at the end of the outer loop, where is all the output stored while the loops are still executing. Perhaps in the memory. Does it mean I can run out of memory if I echo a lot of stuff and only write at the end.
  3. Is 2nd more like the 1st or 3rd. Why is it so different from either.

PS: I have run the above commands a couple of times and found the times to be consistent. So, the differences I see must be due to some real reasons.

  • 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-15T04:39:46+00:00Added an answer on June 15, 2026 at 4:39 am
    1. The first version does a million times the echo $i $j >> /tmp/file, which opens the file for appending, writes to it and closes it.

    2. Doing a million times echo $i $j >&3 differs from one in that it does not open/close the file each time, but writes to file descriptor #3. The exec 3>/tmp/file opens the file for writing and saves the file descriptor as #3. When now a command has its stdout redirected to file descriptor #3 (the effect of the >&3 after the echo), the shell needs to setup this redirection before executing the command and afterwards restore the previous assignment to stdout.

    3. Redirecting the output of the complete loop like this >> /tmp/file is much easier for the shell: It can simply execute the echo command without setting up additional file descriptors. It changes the assignment of stdout just once.

    About buffering: In all three cases the underlying file system will buffer the access to the physical file, so there is no difference at that level. Also most linuxes have a tmpfs mounted on /tmp that makes everything you do a pure memory operation anyway. So You are not measuring IO-performance here but shell command execution performance. You can prove this by increasing the number of bytes written (add a constant value to the line echo prints):

    >/tmp/file ; time for i in {1..1000}; do for j in {1..1000}; do echo "1000000 $i $j" >> /tmp/file; done; done
    
    >/tmp/file ; exec 3>/tmp/file; time for i in {1..1000}; do for j in {1..1000}; do echo "1000000 $i $j" >&3; done; done; exec 3>&-
    
    >/tmp/file ; time for i in {1..1000}; do for j in {1..1000}; do echo "1000000 $i $j"; done; done >> /tmp/file
    

    On my PC this takes just the same time as without the constant “1000000 “, but writes twice as many bytes to the file.

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

Sidebar

Related Questions

Is it possible to combine redirecting output to a file and pipes with ||?
I am redirecting the g++ compiler output(both stderr and stdout) to a file on
After redirecting standard input and output to some files(for example) using freopen how can
I came across some test code which is giving different output when executed on
I am a beginner in UNIX. I am finding some difficulty in input/output redirection.
I have some strange behavior redirecting stdout and stderr from s3cmd. For example: $
For some weird reason, the codes below are first working, then website is redirecting
I've got a script that sets some session values before redirecting to / using
Redirecting to different server but same domain. I have 2 servers on different hosts.
I am writing a script to read a file and replace some characters which

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.