who is faster?

A forum for general discussion of the Python programming language.

who is faster?

Postby vijaykumbhani » Fri Sep 06, 2013 1:16 pm

1. len(fileobject.read())
2. os.path.getsize(filepath)

i think second is faster
why ?
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: who is faster?

Postby micseydel » Fri Sep 06, 2013 4:31 pm

The first one is going to read the entire file into memory. The second one just asks the operating system.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1220
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: who is faster?

Postby vijaykumbhani » Tue Sep 10, 2013 6:15 am

i think , internally OS read file and measure length of file
both are time ~ same
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: who is faster?

Postby micseydel » Tue Sep 10, 2013 6:51 am

One is a O(1) operation, just asking the operating system to look up a small, fixed number of bytes that was recorded as the file size. The other, instead of doing that, does an O(n) operation and in the worst case could cause your computer to halt because it started using swap and is just going terribly, terribly slow.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1220
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: who is faster?

Postby Kebap » Sat Sep 14, 2013 1:48 pm

You can find out answers to such questions yourself by using python timer functionality.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 380
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest