CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Opening Files

 



Pro_4
User

May 5, 2001, 12:13 PM

Post #1 of 7 (822 views)
Opening Files Can't Post

Hey
When opening files you have quite a few options on how to open them. I was wondering why you wouldnt just use +>>file all the time instead of >>file or <file . Is there som problems useing the +>> or speed issues?
Just wondering.

:)

People Engrossed by a Reliable Language


rjoseph
Novice

May 5, 2001, 6:05 PM

Post #2 of 7 (817 views)
Re: Opening Files [In reply to] Can't Post

Each one of the options is different is its own little way - if you have the Camel, 3rd edition, what I am about to say is on page 749. Here are the slight differences:

>>
Cannot read, only write and append to.

+>>
Can read, as well as write and append to.

<
Only read, nothing else.

(There are others, obviously, but those are the three you mentioned). So each is useful in a certain situation. For example, instead of opening a file, reading data from it, closing it, and opening again to write, you can use +>> to read, truncate/seek to clear, and then write again! Fun, isn't it!

Hope this helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 5, 2001, 6:26 PM

Post #3 of 7 (815 views)
Re: Opening Files [In reply to] Can't Post

Well in a way you answered my question. What i wanted to know is if you only wanted to write and append to the end you would use >> . Is there any disadvantages to just useing +>> instead of >>,it would add the feature of reading and kinda makes it easier for you in the future for upgrading the program. Is there problems with using more than necessary ( i.e. speed, security, etc).

I hope i explained it better that time. :)

People Engrossed by a Reliable Language


rjoseph
Novice

May 5, 2001, 7:11 PM

Post #4 of 7 (814 views)
Re: Opening Files [In reply to] Can't Post


In Reply To
Is there problems with using more than necessary ( i.e. speed, security, etc).

Nope, not at all. If there is one thing that Perl is REALLY good at, it is text processing - because that is, in fact, what it was written for in the first place. I use +>> 99% of the time, with no detremental effects!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 6, 2001, 6:06 AM

Post #5 of 7 (812 views)
Re: Opening Files [In reply to] Can't Post

Alright cool, thats what i wanted to know.
:)

People Engrossed by a Reliable Language


rjoseph
Novice

May 7, 2001, 6:39 PM

Post #6 of 7 (798 views)
Re: Opening Files [In reply to] Can't Post

I was thinking about exactly how much quicker using >> compared to +>> is - so I decided to benchmark it!!

Attached is the code that I used, but basically, it has two subs: one opens a file with >> and the other with +>> - they both simply append "hello there!" to the end of a text file. In all of my benchmarks, I ran the script though 400,000 iterations - that means that each subroutine ran four hundred thousand times - so before you run off and change all your +>>'s to >>'s, remember that :). Also, Plus Append means the +>> subroutine, Straight Append the >> routine.

Ok, here is the test from my own computer, an 800 MHz, AMD Thunderbird (256 kb cache) with 256 MB of ram running Slackware 7.1, Kernel 2.2.16 and Perl 5.6.0:

Code
Benchmark: timing 400000 iterations of Plus Append, Straight Append... 
Plus Append: 15 wallclock secs ( 7.44 usr + 8.37 sys = 15.81 CPU) @ 25300.44/s (n=400000)
Straight Append: 13 wallclock secs ( 7.66 usr + 6.05 sys = 13.71 CPU) @ 29175.78/s (n=400000)

Notice how even over 400,000 iterations, the regular append is only about 15% faster than the plus append.

Here is another try, this time on a Celeron 370 MHz (128 kb cache), 128 MB ram, running RedHat 6.1 with kernel 2.2.12-20 and perl 5.005_03:

Code
Benchmark: timing 400000 iterations of Plus Append, Straight Append... 
Plus Append: 42 wallclock secs (19.79 usr + 21.96 sys = 41.75 CPU)
Straight Append: 37 wallclock secs (18.69 usr + 19.09 sys = 37.78 CPU)

Again, 400,000 iterations, same exact code, and it just took longer because that computer is much slower - same results though.

Conclusion: unless you are going to be opening and closing a file 400,000 times, speed with Perl's open operators doesn't matter a bit!

Next time: benchmarking ALL of the open operators (haha, yeah right :)


r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 8, 2001, 10:41 AM

Post #7 of 7 (789 views)
Re: Opening Files [In reply to] Can't Post

WOW that is really interesting. I will keep that in mind but i really doubt that i will be running it 400,000 times so i really dont think i have much to worry about. But thanks for that really interesting tid bit of info.

:)

People Engrossed by a Reliable Language

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives