Skip to content

Instantly share code, notes, and snippets.

@amirmazmi
Created March 6, 2025 17:38
Show Gist options
  • Save amirmazmi/ed4a5a491fd6b601b1a9ac34065ae2ea to your computer and use it in GitHub Desktop.
Save amirmazmi/ed4a5a491fd6b601b1a9ac34065ae2ea to your computer and use it in GitHub Desktop.

Generate 4 character permutations from 92 chars

92P4 = 71,639,296



Julia

4chars.jl


keyspace = raw"!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
chars = 3

perms = Iterators.product(fill(keyspace, chars)...) |> collect |> vec

perms |> size |> println
# perms[1000:10005] |> println


single thread

$ time julia -t1 4chars.jl
(71639296,)

real	0m0.748s
user	0m0.548s
sys	0m0.605s


$ time julia -t1 4chars.jl
(71639296,)

real	0m0.723s
user	0m0.552s
sys	0m0.576s


14 threads - ***hyperthreading

$ time julia -t14 4chars.jl
(71639296,)

real	0m0.681s
user	0m0.584s
sys	0m0.577s


$ time julia -t14 4chars.jl
(71639296,)

real	0m0.692s
user	0m0.769s
sys	0m0.528s



Python 3.9

single threaded (standard python)

Numpy

numpytest.py


import numpy as np

char_slot = 4
charlist = "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
arr_char = np.array(list(charlist))


arr_add = arr_char
for k in range(char_slot - 1):
    arr_add = np.char.add( np.repeat( arr_char, arr_add.shape[0]),
                           np.tile( arr_add, (arr_char.shape[0]))
                           )

print(arr_add.shape)
# print(arr_add.shape[1000:1005])


$ time python numpytest.py
(71639296,)
real	0m30.379s
user	0m30.026s
sys     0m1.296s


$ time python numpytest.py
(71639296,)
real	0m30.392s
user	0m30.106s
sys     0m1.228s



Itertools

itertest.py

import itertools

charlist = "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
charlen = 4

ls = list(itertools.product(charlist,repeat=charlen))

print( len(ls))
#print( ls[1000:1005])


$ time python itertest.py
71639296

real	0m6.348s
user	0m5.080s
sys     0m1.268s


$ time python itertest.py
71639296

real	0m6.429s
user	0m5.181s
sys     0m1.248s



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment