Jump to content
Sign in to follow this  

Suggestions for optimizing code?

Recommended Posts

Hello, I'm generating recipes. I've generated a list of all possible combinations of 2, 3, 4 and 5 ingredients , and I have a list of all the percents that would be usable.  I've put together the following code, but with the 4s and 5s there are like 7 billion combinations. Is there a faster way to generate the combinations? 

;78k elements
_FileReadToArray(@ScriptDir &"\all-percents.csv", $percents, Default, ",")
; 100k elements
_FileReadToArray(@ScriptDir &"\all-combinations.csv", $combos, Default, ",")

; text files that the soap recipes will be written to
Local $allRecipes = @ScriptDir & "\all-recipes.txt"

for $z = 1 to $combos[0][0]-1
    $arrayLen = 0
    ; this counts all the elements in the line of the array (2, 3, 4, 5) and stores the number in $arrayLen
    for $e = 0 to 5
        if $combos[$z][$e] = "" Then
            ; if it is blank, exit the loop
            $arrayLen = $arrayLen + 1

    Switch $arrayLen
        Case 1

            for $i = 0 to 0
                ; write the names & percents
                $write = $combos[$z][0]& "," & $percents[$i][0]
                FileWriteLine($allRecipes, $write)

        Case 2
            ; on the list of percents 1 through 380 is the list of 2 numbers that add to 100
            for $i = 1 to 380

                $write = $combos[$z][0]& "," & $percents[$i][0] & "," & $combos[$z][1] & "," & $percents[$i][1]
                FileWriteLine($allRecipes, $write)

        Case 3
            ; array elements that match the 3 number combinations that add to 100
            for $i = 380 to 3800
                $write = $combos[$z][0]& "," & $percents[$i][0] & "," & $combos[$z][1]& "," & $percents[$i][1] & "," & $combos[$z][2]& "," & $percents[$i][2]
                FileWriteLine($allRecipes, $write)

        Case 4
            ; array elements that match the 4 number combinations that add to 100
            for $i = 3801 to 23177
                 $write = $combos[$z][0]& "," & $percents[$i][0] & "," & $combos[$z][1]& "," & $percents[$i][1] & "," & $combos[$z][2]& "," & $percents[$i][2]& "," & $combos[$z][3]& "," & $percents[$i][3]
                FileWriteLine($allRecipes, $write)

        Case 5
            ; array elements that match the 5 number combinations that add to 100
            for $i = 23178 to 100673
                $write = $combos[$z][0]& "," & $percents[$i][0] & "," & $combos[$z][1]& "," & $percents[$i][1] & "," & $combos[$z][2]& "," & $percents[$i][2]& "," & $combos[$z][3]& "," & $percents[$i][3]& "," & $combos[$z][4]& "," & $percents[$i][4]
                FileWriteLine($allRecipes, $write)


Share this post

Link to post
Share on other sites


can you post an example file?

i actually dont get it - seems rly complicated for an easy function but i didnt really understand what you want :o  would help to understand it :) 



EDIT: I mean there are so much undefinied numbers i actually don't know where they come from in your



Edited by Aelc

why do i get garbage when i buy garbage bags? <_<

Share this post

Link to post
Share on other sites

Sure so for example you would have the letters A through L and all permutations of them in. So you would have:


and so forth all the way through the 5 combinations of the ingredients.  Now the percent file has all the percents in increments of 5 like

5, 95
10, 90 etc all the way up to the 5 combinations of percents.

The script goes through and merges them together. So the final output would look like

a, 5, b, 95
a, 10, b, 80

and once it goes through all the 2 number combinations for the 2 letter combinations it repeats it for the next set of letters. Then the 3 letters / 3 numbers, 4 letters / 4 numbers, 5 letters/ 5 numbers.

a, 5, c, 95
a, 10, c, 80
a, 5, d, 95

which the above script works, except that it is going to take something like 2 months running 24/7 to generate all the combinations.

Share this post

Link to post
Share on other sites


Just why are you doing this massive exercise in combining "ingredients"? What exactly are you going to do with this multitude of different "recipes"?


P.S. And just to be absolutely clear - this is the Mod team determining the legality of the thread, so everyone else please keep out.


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...