+ 1

Fast IO in python

I have optimized my code in python. still, it taking a lot of time. https://code.sololearn.com/cmLigX7sm0yB/#py Can anyone help me to optimize it a little bit more?

17th Jul 2020, 6:33 AM
KUMAR SHANU
KUMAR SHANU - avatar
7 Respostas
+ 4
map by itself is slightly faster than a comparable list comprehension. So your first use of map is faster than the use of list comprehension. However, your second use is slower, because you're first mapping to each element to an int then converting the map object to a list. Instead, you can use list comprehension to do both during the process of creating the list. Then you could also use dictionary comprehension to create your arr_sum instead of creating an empty dict and then looping through a range to fill it. _, Q = map(int, stdin.readline().split()) # N isn't used arr = [int(x) for x in stdin.readline().split()] arr_sum = {i:sum(arr[:i+1]) for i in range(len(arr))} Note: stdin.readline() doesn't work in the SL playground, so you'd need to use input() to run the code here instead. This code below can replace your if-else statement def run_if_true(arr_sum, R, L): stdout.write(str((arr_sum[R-1])//(R - L + 1))) def run_if_false(arr_sum, R, L): stdout.write(str((arr_sum[R-1] - arr_sum[L-2])//(R - L + 1))) (run_if_false, run_if_true)[L == 1]() The code replaces the if-else with a tuple, where the condition is the index. If false it will run the 1st bit of code (what was the else block), if true then it will run the 2nd bit of code. So, in theory, these changes should be faster, but results may be negligible at best depending on the size of the list.
17th Jul 2020, 8:02 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
When comparing; map(int, input()) to [int(x) for x in input()] Yes, map is slightly, but measurably faster when calling a pre-defined or built-in function. For more complex operations list will be faster. When taking the speed measurements of each for comparison, they need to be doing the same thing. Obviously, if you compared a significantly more complex map() usage to a simple list usage or vice versa the results would be skewed. https://stackoverflow.com/questions/1247486/list-comprehension-vs-map https://www.google.com/amp/s/www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-map-vs-list-comprehension/amp/
17th Jul 2020, 12:30 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ChaoticDawg Excellent.
17th Jul 2020, 12:40 PM
Emanuel Maliaño
Emanuel Maliaño - avatar
+ 2
``` from sys import stdin, stdout N, Q = map(int, stdin.readline().split()) arr = [0] for inp in stdin.readline().split(): arr.append(int(inp) + arr[-1]) for line in stdin.readlines(): L, R = map(int, line.split()) stdout.write(str((arr[R] - arr[L-1])//(R - L + 1))) stdout.write('\n') ```
18th Jul 2020, 3:47 PM
KUMAR SHANU
KUMAR SHANU - avatar
+ 1
Map is faster than list comprehension. Emanuel Maliaño
17th Jul 2020, 7:56 AM
KUMAR SHANU
KUMAR SHANU - avatar
+ 1
ChaoticDawg So is a map conversion faster than comprehension lists? My answer is based on what I saw in the Pycon a couple of years ago but it is good to know that in things as simple as the case of the conversion it is more a quick map.
17th Jul 2020, 12:17 PM
Emanuel Maliaño
Emanuel Maliaño - avatar
- 1
Use list comprenhesion instead of map.
17th Jul 2020, 6:52 AM
Emanuel Maliaño
Emanuel Maliaño - avatar