473,748 Members | 5,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which is More Efficient?

I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it. So which of the
following would be more efficient, knowing that l is a list and size is
a number?

l=l[:size]
del l[size:]

If it makes a difference, everything in the list is mutable.

May 18 '06 #1
16 1682
Measure it and find out. Sounds like a little investment in your time
learning how to measure performance may pay dividends for you.

May 18 '06 #2
"Dustan" <Du**********@g mail.com> writes:
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it.
Profile your program and find the precise parts that are the
slowest. Attempting to optimise before that is a waste of your time.
So which of the following would be more efficient, knowing that l is
a list and size is a number?

l=l[:size]
del l[size:]


Which one is more efficient in your program, when you profile its
performance?

<URL:http://docs.python.org/lib/profile.html>

--
\ "Working out the social politics of who you can trust and why |
`\ is, quite literally, what a very large part of our brain has |
_o__) evolved to do." -- Douglas Adams |
Ben Finney

May 18 '06 #3
Dustan wrote:
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it. So which of the
following would be more efficient, knowing that l is a list and size is
a number?

l=l[:size]
del l[size:]
since you have the program, it shouldn't that hard to test the
two alternatives, should it ?

(in theory, del should be faster in most cases, since it avoids
creating another object. but the only way to tell for sure is
to try it out).
If it makes a difference, everything in the list is mutable.


the only difference between mutable and immutable objects in Python
is that mutable objects have methods that let you modify the object
contents, while immutable objects don't have such methods.

</F>

May 18 '06 #4
1. Think about it. The first case will make a new list and copy "size"
*objects. When the assignment happens, the old list has its reference
count decremented. Not very memory-friendly. The second case merely
truncates the existing list in situ. Bit hard to imagine how the first
case could ever be faster than the second case. You might like to read
the source code. The file that you are looking for is listobject.c.
2. Measure it.
3. Unless you are deliberately parodying b1**@aol.com, don't use
"L".lower() as a variable name.
4. Try reading this list / newsgroup more often -- (a) this topic (or a
closely related one) was covered within the last week or so (b) you
might notice abuse like (3) above being hurled at others and avoid
copping your share.
HTH,
John

May 18 '06 #5

John Machin wrote:
1. Think about it. The first case will make a new list and copy "size"
*objects. When the assignment happens, the old list has its reference
count decremented. Not very memory-friendly. The second case merely
truncates the existing list in situ. Bit hard to imagine how the first
case could ever be faster than the second case. You might like to read
the source code. The file that you are looking for is listobject.c.
That's what I thought. I wasn't sure exactly what del actually does.
2. Measure it.
Tell me how and I will; I'm not nearly that much of a geek
unfortunately.
3. Unless you are deliberately parodying b1**@aol.com, don't use
"L".lower() as a variable name.
Don't understand what you mean by this, but I didn't actually use 'l'
as a variable name; that was just an example.
4. Try reading this list / newsgroup more often
I follow too many newsgroups as it is, when I've got plenty of other
stuff to do.
-- (a) this topic (or a
closely related one) was covered within the last week or so (b) you
might notice abuse like (3) above being hurled at others and avoid
copping your share.

HTH,
John


May 18 '06 #6

Fredrik Lundh wrote:
Dustan wrote:
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it. So which of the
following would be more efficient, knowing that l is a list and size is
a number?

l=l[:size]
del l[size:]
since you have the program, it shouldn't that hard to test the
two alternatives, should it ?

(in theory, del should be faster in most cases, since it avoids
creating another object. but the only way to tell for sure is
to try it out).
If it makes a difference, everything in the list is mutable.


the only difference between mutable and immutable objects in Python
is that mutable objects have methods that let you modify the object
contents, while immutable objects don't have such methods.


And it can be referenced by different variables. What I was saying was
that the contents weren't being copied over; it was only the list that
was being copied in the first statement.

</F>


May 18 '06 #7
"Dustan" <Du**********@g mail.com> writes:
John Machin wrote:
2. Measure it.


Tell me how and I will; I'm not nearly that much of a geek
unfortunately.


You've already been told. Here it is again:

<URL:http://docs.python.org/lib/profile.html>

--
\ "I don't know half of you half as well as I should like, and I |
`\ like less than half of you half as well as you deserve." -- |
_o__) Bilbo Baggins |
Ben Finney

May 18 '06 #8
Dustan wrote:
2. Measure it.


Tell me how and I will; I'm not nearly that much of a geek
unfortunately.


do you have to be a geek to be able to measure how much time
something takes?

</F>

May 18 '06 #9
Fredrik Lundh wrote:
Dustan wrote:
2. Measure it.
Tell me how and I will; I'm not nearly that much of a geek
unfortunately.


do you have to be a geek to be able to measure how much time
something takes?


Obviously it takes a geek to know you have to time it, as opposed to
any other task you could be talking about.

</F>


May 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2917
by: wogston | last post by:
(A) template <typename T> metainline foo<T> lerp(const foo<T>& a, const foo<T>& b, const T& time) { foo<T> v; repeat< 4,exec_mul<T> >::exec(v,a,b,time); return v; }
9
4235
by: Randell D. | last post by:
Folks, I have a large amount of values to store (we're talking tens, if not hundreds of bytes). I need this for a client side application - ignore the security consequences for the moment - however my question is, which is more performance effective, or easier on a clients resources? I mean - if I have several <input type=hidden> tags with my values that I can reference, would this have a greater overhead then, for example, having an...
10
3518
by: Amit | last post by:
Which is more efficient and why? p++ or ++p. Thanks.
12
3232
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
1
1384
by: JJ | last post by:
I need to do a SELECT query that joins four tables. The largest of the tables could eventually have over 1 million records. I need to do a SELECT that returns a maximum of 1000 records which will display on multiple pages. Which way would be more efficient.... 1. Do the SELECT and LIMIT it to 1000 records, then relying on MySQL's caching, do a data seek to jump to the record that will begin each page. As I understand it, MySQL will do...
2
2631
theGeek
by: theGeek | last post by:
I always wonder which one of join or subquery should I be using to solve a particuar problem so that I get the correct result set in minimum time. It usually happens that I can write a query quickly using subqueries (I think I happen to think that way more easily than join way :)) but I see others making use of joins all the time. May be that they can think of a query more easily using joins. I am saying that because I don't usually see much of...
19
1889
by: santanu mishra | last post by:
Hi , I am stuck with a requirement from my client to change the date format from mm/dd/yy to dd/mm/yy .If any body can help me out with this regard as its very much urgent. Regards, Santanu
50
4492
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
6
1266
by: Christopher | last post by:
I've seen various ways of doing this, but what I want is to be able to take a base class pointer and know which derived class to cast to. For example I am going to make a base light class, that has a position and intensity. A derived classes class may be a point light which also has a falloff value. The derived class has all the base class methods and data in common, but has the additional falloff value get/set methods.
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.