Python Forum
Loop to find the best combination/score - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Loop to find the best combination/score (/thread-39077.html)

Pages: 1 2 3


RE: Loop to find the best combination/score - deanhystad - Jan-05-2023

I wrote a quick-n-dirty test by hand just to see how quick it would be. A little optimization will make it much faster. I wrote it in C, so the players list is dynamically allocated array of pointers. An array of player structs would be faster as would a C++ vector. I am using a new compiler and trying to learn how to wirte C/C++ using VSCode instead of Visual Studio or the gnu compiler and command line tools. Not sure what compiler optimization settings are, but I doubt that my executable is "highly optimized".

I modified my code to use structs and arrays of structs instead of pointers. This allows certain optimizations by the compiler. When I run my new, very ugly but optimized code it takes 2 minutes to find the best team that can be made from 400 players. I ran my timing test multiple times because the player list will affect timing. The fastest time was 1 minute 42 seconds. The worst was 3 minutes 17 seconds.

A really fun comparison is looking at the time for 60 teams. You posted that your numpy code for solving the problem ran for 3366 seconds to come up with a solution. My C program solves it in 0.016 seconds. 210,375 times faster. The C program is also 724 times faster than my solution that used combinations and 47 times faster than my Python program that uses loops.


RE: Loop to find the best combination/score - KoinKoin - Jan-05-2023

Thanks for all your tests.

I started to do a C++ program, so far I succeeded to get my 400 players almost instantly with some rand% for the values.

Now I am looking for the loop/combinations, my few tests are taking too long at the moment (or bad alloc), need to do more research for that :)