There's An OPTIMIZED Way To Do THIS In Python (ft. enumerate)
Вставка
- Опубліковано 29 чер 2024
- Here's a quick optimization that you can perform in Python with enumerate().
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
00:00 Intro
00:10 enumerate()
01:38 The tests
03:53 A good optimisation?
04:14 That’s it
For the occasions where "start" with enumerate has been really useful for me, I define the start as a variable because it depends on some other conditions. Then I refer to that variable via "start=". I think having the start value somewhere other than with "start=" is less readable, but each to their own, I guess.
Out of curiosity, do you mind sharing an example of where you would do that? Just curious!
Loved the video! Which IDE is that tho?
Thanks for the information
Correct me if I am incorrect, could you not have just done the following for the enumerate function?:
temp_list = list(enumerate(values, start=1))
That's the correct code golf yes. But then you also have to do it for the normal_func.
look at this code below
for key, group by groupby(enumerate(numbers,start=1), lambda x: x[1] - x[0])
i can't set i + 1) use start instead, it more readable
Is it the same if you first time normal and then the “start=1” solution?
It’s not very accurate to benchmark like this.
First timed would be in worst condition, starting with the system that is not yet heated up.
Ideally this should be 2 separate programs and you should start it separate with a propped benchmarking tool that performs several heat up runs and then several measurement runs and calculate the average.
Yeah I tried swapping them and the result remained the same. But I'll look into using more accurate benchmark tools, thanks for the recommendation!
@@Indently np! I just like similar simple benchmarks myself. I enjoy doing different kinds of comparisons and I lately use benchmarking tools, don’t want to recommend anything just in order to not advertise 🙂. But it’s much more accurate from my experience.
You're more than welcome to recommend any! Some people (including myself) love reading experiences with certain tools from others.
@@Indently I personally like hyperfine, yes it’s not python native its a standalone os tool, but it’s open source and it worked for me perfectly as I often compare different languages or different versions of languages.
It looks super cool to me, I might try it in my next video! I'll play around with it first. Thanks for sharing!
Writing start= is especially better if you use the value more than once.
How is it confusing to read?
Why I need to use that "enumerate" function instead of "for _ in sth" with definition of list_index = 0 and in the cycle I can just increment that index. Indexes often are not required in iterations.
Because Python is not C. For loops in Python are very slow compared to C. Enumerate is faster.
You could do that, but generally your python code will be faster if you use the built-in functions when available. The reason being that these functions are frequently implemented in C, and therefore can do the job faster than pure Python.
Python 11 shaving off time, by making both implementations slower 🙃 @ 3:41
What are you talking about? 3.10 ran both of them slower than 3.11.
the first run was on python 11, the slower run was in python 10.