Arsalan Ali Profile picture
Oct 6, 2022 โ€ข 8 tweets โ€ข 4 min read โ€ข Read on X
Ever wondered what algorithm can sum a list/array the fastest in Python? Here's a surface level overview

A Thread ๐Ÿงต๐Ÿ‘‡

#python3 #pythonic #pythonprogramming #programming #Algorithms #DataScience Image
First of we generated a random list of "1000000" numbers to feed all the algorithms.

This very first algorithm uses the built-in sum method. Its the easiest one to implement. Image
Second algorithm is just the traditional approach of creating a variable and then using a for loop to add the sum into it. Image
Third algorithm isn't basically anything new. As a matter of fact its written to slow down the process because sum method already iterates on a list/array. So giving it a for loop should slow it down. It's just for experimental purposes. Image
Fourth algorithm is based on reduce method. You need to specify some sort of function in it. You can use it without defining a function but for the sake of consistency we've written it like this. Image
Lastly we've used the Numpy Sum method in the fifth algorithm. Image
As you can see from the results, Numpy Sum is the winner here. It beats the built-in Sum method by quite a margin. Meanwhile our Sum Loop approach performs the worst as expected. Reduce and Loop algorithms are almost similar in performance. Image
Hope you liked the surface-level review and it helped you to choose the better algorithm for your project next time. ๐Ÿ˜Š๐Ÿฅณ

If you want more content like this, make sure to follow me.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Arsalan Ali

Arsalan Ali Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @arslanchaos

Sep 16, 2022
Here's a list of almost ~all regression models used in machine learning. Make sure to try them out.

A Thread ๐Ÿงต๐Ÿ‘‡

#MachineLearning #regression #codanics #ArtificialIntelligence #DataScience #analysis #DataAnalytics #model #algorithm #pakdatasci Image
Image
Image
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(