Re: list comparison vs integer comparison, which is more efficient?
On 04.01.2015 13:17, austin aigbe wrote Hi Terry, No difference between the int and list comparison in terms of the number of calls(24) and time (0.004s). Main part is the repeated call to sqrt(). However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. How often did you run your measurement? 4ms is not a whole lot and can easily be skewed by sudden system load or other noise. You should call the function more often and/or repeat the measurement several times before coming to a judgement (except, possibly, that it doesn’t matter). cheers, jwi -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sunday, January 4, 2015 8:12:10 AM UTC+1, Terry Reedy wrote: On 1/3/2015 6:19 PM, austin aigbe wrote: I am currently implementing the LTE physical layer in Python (ver 2.7.7). For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: Integer comparison: bit_pair as an integer value before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = (self.sbits[i*2] 1) | self.sbits[i*2+1] if bit_pair == 0: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 1: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == 2: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 3: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r List comparison: bit_pair as a list before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] if bit_pair == [0,0]: r.append() elif bit_pair == [0,1]: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == [1,0]: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [1,1]: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r Wrong question. If you are worried about efficiency, factor out all repeated calculation of constants and eliminate the multiple comparisons. sbits = self.sbits a = 1.0 / math.sqrt(2) b = -a points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) complex(math.sqrt(2),1/math.sqrt(2)) def mp_qpsk(self): r = [points[sbits[i]*2 + sbits[i+1]] for i in range(0, self.nbits, 2)] return r -- Terry Jan Reedy Cool. Thanks a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sunday, January 4, 2015 12:20:26 PM UTC+1, austin aigbe wrote: On Sunday, January 4, 2015 8:12:10 AM UTC+1, Terry Reedy wrote: On 1/3/2015 6:19 PM, austin aigbe wrote: I am currently implementing the LTE physical layer in Python (ver 2.7.7). For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: Integer comparison: bit_pair as an integer value before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = (self.sbits[i*2] 1) | self.sbits[i*2+1] if bit_pair == 0: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 1: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == 2: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 3: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r List comparison: bit_pair as a list before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] if bit_pair == [0,0]: r.append() elif bit_pair == [0,1]: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == [1,0]: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [1,1]: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r Wrong question. If you are worried about efficiency, factor out all repeated calculation of constants and eliminate the multiple comparisons. sbits = self.sbits a = 1.0 / math.sqrt(2) b = -a points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) complex(math.sqrt(2),1/math.sqrt(2)) def mp_qpsk(self): r = [points[sbits[i]*2 + sbits[i+1]] for i in range(0, self.nbits, 2)] return r -- Terry Jan Reedy Cool. Thanks a lot. Hi Terry, No difference between the int and list comparison in terms of the number of calls(24) and time (0.004s). Main part is the repeated call to sqrt(). However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. Why is this? Integer comparison profile result: p = pstats.Stats('lte_phy_mod.txt') p.strip_dirs().sort_stats(-1).print_stats() Sun Jan 04 12:36:32 2015lte_phy_mod.txt 24 function calls in 0.004 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0040.0040.0040.004 lte_phy_layer.py:16(module) 10.0000.0000.0000.000 lte_phy_layer.py:20(Scrambling) 10.0000.0000.0000.000 lte_phy_layer.py:276(LayerMapping) 10.0000.0000.0000.000 lte_phy_layer.py:278(Precoding) 10.0000.0000.0000.000 lte_phy_layer.py:280(ResourceElementMapping) 10.0000.0000.0000.000 lte_phy_layer.py:282(OFDMSignalGenerator) 10.0000.0000.0000.000 lte_phy_layer.py:65(Modulation) 10.0000.0000.0000.000 lte_phy_layer.py:71(__init__) 10.0000.0000.0000.000 lte_phy_layer.py:87(mp_qpsk) 10.0000.0000.0000.000 {len} 80.0000.0000.0000.000 {math.sqrt} 40.0000.0000.0000.000 {method 'append' of 'list' objects} 10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects} 10.0000.0000.0000.000 {range} pstats.Stats instance at 0x028F3F08 List comparison: import pstats p = pstats.Stats('lte_phy_mod2.txt') p.strip_dirs().sort_stats(-1).print_stats() Sun Jan 04 12:57:24 2015lte_phy_mod2.txt 24 function calls in 0.004 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0040.0040.0040.004 lte_phy_layer.py:16(module) 10.0000.0000.0000.000 lte_phy_layer.py:20(Scrambling) 10.0000.0000.0000.000 lte_phy_layer.py:276(LayerMapping) 10.0000.0000.0000.000 lte_phy_layer.py:278(Precoding) 10.0000.0000.0000.000 lte_phy_layer.py:280(ResourceElementMapping) 10.0000.0000.0000.000 lte_phy_layer.py:282(OFDMSignalGenerator) 10.0000.0000.0000.000 lte_phy_layer.py:65(Modulation) 10.0000.0000.0000.000
Re: list comparison vs integer comparison, which is more efficient?
Am 04.01.15 um 13:17 schrieb austin aigbe: However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. Why is this? These times are way too short for conclusive results. Typically, the OS timer operates with a millisecond resolution. You need to run a benchmark at least for a second to get reliable information about timing. INstead of 24 times, call your function 2 times in loop. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On 04/01/2015 12:22, Christian Gollwitzer wrote: Am 04.01.15 um 13:17 schrieb austin aigbe: However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. Why is this? These times are way too short for conclusive results. Typically, the OS timer operates with a millisecond resolution. You need to run a benchmark at least for a second to get reliable information about timing. INstead of 24 times, call your function 2 times in loop. Christian Maybe using a custom built tool such as https://docs.python.org/3/library/timeit.html#module-timeit ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sun, Jan 4, 2015 at 11:17 PM, austin aigbe eshik...@gmail.com wrote: However, it took a shorter time (0.004s) with 24 function calls than your code (0.005s) which took just 13 function calls to execute. Why is this? That looks to me like noise in your stats. One ULP in timing stats? Not something to base *anything* on. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On Sun, Jan 4, 2015 at 10:19 AM, austin aigbe eshik...@gmail.com wrote: I would like to know which is more efficient to use, between an integer comparison and a list comparison: You can test them with the timeit module, but my personal suspicion is that any difference between them will be utterly and completely dwarfed by all your sqrt(2) calls in the complex constructors. If you break those out, and use a tuple instead of a list, you could write this very simply and tidily: bits = { (0,0): complex(1/math.sqrt(2),1/math.sqrt(2)), (0,1): complex(1/math.sqrt(2),-1/math.sqrt(2)), (1,0): complex(-1/math.sqrt(2),1/math.sqrt(2)), (1,1): complex(-1/math.sqrt(2),-1/math.sqrt(2)), } # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] r.append(bits[tuple(bit_pair)]) return r At this point, your loop looks very much like a list comprehension in full form, so you can make a simple conversion: # From itertools recipes # https://docs.python.org/3/library/itertools.html def pairwise(iterable): s - (s0,s1), (s1,s2), (s2, s3), ... a, b = tee(iterable) next(b, None) return zip(a, b) # Replace zip() with izip() for the Python 2 equivalent. def mp_qpsk(self): return [bits[pair] for pair in pairwise(self.sbits)] How's that look? I don't care if it's faster or not, I prefer this form :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
list comparison vs integer comparison, which is more efficient?
Hi, I am currently implementing the LTE physical layer in Python (ver 2.7.7). For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: Integer comparison: bit_pair as an integer value before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = (self.sbits[i*2] 1) | self.sbits[i*2+1] if bit_pair == 0: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 1: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == 2: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 3: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r List comparison: bit_pair as a list before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] if bit_pair == [0,0]: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [0,1]: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == [1,0]: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [1,1]: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: list comparison vs integer comparison, which is more efficient?
On 1/3/2015 6:19 PM, austin aigbe wrote: I am currently implementing the LTE physical layer in Python (ver 2.7.7). For the qpsk, 16qam and 64qam modulation I would like to know which is more efficient to use, between an integer comparison and a list comparison: Integer comparison: bit_pair as an integer value before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = (self.sbits[i*2] 1) | self.sbits[i*2+1] if bit_pair == 0: r.append(complex(1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 1: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == 2: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == 3: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r List comparison: bit_pair as a list before comparison # QPSK - TS 36.211 V12.2.0, section 7.1.2, Table 7.1.2-1 def mp_qpsk(self): r = [] for i in range(self.nbits/2): bit_pair = self.sbits[i*2:i*2+2] if bit_pair == [0,0]: r.append() elif bit_pair == [0,1]: r.append(complex(1/math.sqrt(2),-1/math.sqrt(2))) elif bit_pair == [1,0]: r.append(complex(-1/math.sqrt(2),1/math.sqrt(2))) elif bit_pair == [1,1]: r.append(complex(-1/math.sqrt(2),-1/math.sqrt(2))) return r Wrong question. If you are worried about efficiency, factor out all repeated calculation of constants and eliminate the multiple comparisons. sbits = self.sbits a = 1.0 / math.sqrt(2) b = -a points = (complex(a,a), complex(a,b), complex(b,a), complex(b,b)) complex(math.sqrt(2),1/math.sqrt(2)) def mp_qpsk(self): r = [points[sbits[i]*2 + sbits[i+1]] for i in range(0, self.nbits, 2)] return r -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan wrote: Obviously it takes a geek to know you have to time it, as opposed to any other task you could be talking about. wasn't the original question my program uses a lot of CPU, and I want to make it more efficient ? what does a lot of CPU and more efficient mean to you, and how do you know that your program uses a lot of CPU ? /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Fredrik Lundh wrote: Dustan wrote: Obviously it takes a geek to know you have to time it, as opposed to any other task you could be talking about. wasn't the original question my program uses a lot of CPU, and I want to make it more efficient ? what does a lot of CPU and more efficient mean to you, and how do you know that your program uses a lot of CPU ? The task manager says CPU Usage: 100% when the program is running, and only when the program is running. Efficiency is a measure of 2 things: CPU usage and time. If you measure just time, you're not necessarily getting the efficiency. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan wrote: Fredrik Lundh wrote: Dustan wrote: Obviously it takes a geek to know you have to time it, as opposed to any other task you could be talking about. wasn't the original question my program uses a lot of CPU, and I want to make it more efficient ? what does a lot of CPU and more efficient mean to you, and how do you know that your program uses a lot of CPU ? The task manager says CPU Usage: 100% when the program is running, and only when the program is running. Efficiency is a measure of 2 things: CPU usage and time. If you measure just time, you're not necessarily getting the efficiency. /F By the way, I've only been programming for a year or so (probably 18 months at the most). I'm sure that can't label me as a 'newbie', but at least consider that before you criticize me like you did when I asked about scientific notation. -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: The task manager says CPU Usage: 100% when the program is running, and only when the program is running. Efficiency is a measure of 2 things: CPU usage and time. If you measure just time, you're not necessarily getting the efficiency. A lot of people, when they say 'uses a lot of CPU' are leaving off 'time'. I.e., CPU usage is pretty much talked about in terms of cycles, which is roughly utilization*time. Profiling tools often report both clock time and cpu time. Cpu time is a rough analog for cycles, clock time is self explanatory. max -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan wrote: The task manager says CPU Usage: 100% when the program is running, and only when the program is running. Efficiency is a measure of 2 things: CPU usage and time. If you measure just time, you're not necessarily getting the efficiency. are you for real? /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Fredrik Lundh wrote: Dustan wrote: The task manager says CPU Usage: 100% when the program is running, and only when the program is running. Efficiency is a measure of 2 things: CPU usage and time. If you measure just time, you're not necessarily getting the efficiency. are you for real? And what exactly is that supposed to mean? /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan wrote: Fredrik Lundh wrote: are you for real? And what exactly is that supposed to mean? The obscurity in that communication is probably caused by the instance of the effbot with which you have been corresponding having been invoked with mildmannered=True -- apparently this is not the default value for that arg and the constraints so imposed can lead to lack of precision in the output :-) HTH, John -- http://mail.python.org/mailman/listinfo/python-list
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Measure it and find out. Sounds like a little investment in your time learning how to measure performance may pay dividends for you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 [EMAIL PROTECTED], 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 [EMAIL PROTECTED], 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
Dustan [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Which is More Efficient?
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 -- http://mail.python.org/mailman/listinfo/python-list