Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more efficient implementation of runMarkov #1068

Open
jwaldmann opened this issue Jan 28, 2024 · 0 comments
Open

more efficient implementation of runMarkov #1068

jwaldmann opened this issue Jan 28, 2024 · 0 comments

Comments

@jwaldmann
Copy link
Contributor

jwaldmann commented Jan 28, 2024

(will make a PR, just writing down the observation and idea now)

current implemenation is inefficient in several ways (basically, every usage of the list type for storing data for repeated access is questionable)

runMarkov n tp xi seed = reverse -- computes full list before outputting first element
  $ (iterate (markovStep $ renorm) [xi])!! (n-1) where
  markovStep tp' xs = (fromJust
   $ findIndex (r <=) -- linear cost for searching 
 $ scanl1  (+) (tp'!!(head xs) -- linear cost for accessing the matrix
  )) : xs 
 where
    r = timeToRand $ seed + (fromIntegral . length -- computes length each time
  ) xs / fromIntegral n
  renorm = [ map (/ sum x) x | x <- tp ]

of course, this is all quite irrelevant if the set of states is small (e.g., small interval of notes). But perhaps it is not, e.g., if you want to keep previous 2 notes in the state as well.

Ideas for a better implemetation

  • to avoid linear findIndex, use binary search. This only helps when acces by index is fast, so ...
  • instead of [[Double]], use Vector (Vector Double)

My implementation achieves these timings:

tidal> m w = [[ abs(x-y) | y <-[0 .. w-1]] | x <- [0 .. w-1]]

tidal> e = 5 ; w = 10

tidal> drop (10^e) $ runMarkov  (10^e + 10) (m w) 0 0
[6,9,6,3,7,2,7,3,8,3]
(18.34 secs, 162,666,416 bytes)

tidal> drop (10^e) $ runMarkov' (10^e + 10) (m w) 0 0
[6,9,6,3,7,2,7,3,8,3]
(0.18 secs, 87,951,624 bytes)

NB - output also shows that results are identical

jwaldmann pushed a commit to jwaldmann/Tidal that referenced this issue Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant