Old 06-28-2020, 11:16 AM   #1
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 295
Default What cause this script to run slower and slower?

I run this script in a blank project that only with 1 track and 1 selected media item. Then using loop and action: Item edit: Move duplicate of item to edit cursor
to copy the item many times.

Code:
function msg(value)
  reaper.ShowConsoleMsg(tostring(value) .. "\n")
end

reaper.Undo_BeginBlock()
reaper.PreventUIRefresh(1)
count=0
local time_previous=os.clock()  --start time

for i=1, 10000 do
  reaper.Main_OnCommand(41309, 0)  --move item's duplicate
  --move edit cursor 5 seconds later
  reaper.SetEditCurPos(reaper.GetCursorPosition()+5, 0, 0)  
  count=count+1
  if count%1000==0 then  --record every 1000 times
    local time_now=os.clock()
    msg('Creat 1000 more items cost '..time_now-time_previous)
    time_previous=time_now  --update start time
  end
end

reaper.PreventUIRefresh(-1)
reaper.Undo_EndBlock('', -1)
The result is
Code:
Creat 1000 more items cost 0.016999999999825
Creat 1000 more items cost 0.050999999999476
Creat 1000 more items cost 0.095000000000255
Creat 1000 more items cost 0.15499999999975
Creat 1000 more items cost 0.20600000000013
Creat 1000 more items cost 0.25799999999981
Creat 1000 more items cost 0.30300000000079
Creat 1000 more items cost 0.36599999999999
Creat 1000 more items cost 0.41599999999926
Creat 1000 more items cost 0.46700000000055
If I change it from 10000 to 20000, the result is
Code:
Creat 1000 more items cost 0.016999999999825
Creat 1000 more items cost 0.052999999999884
Creat 1000 more items cost 0.094000000000051
Creat 1000 more items cost 0.14100000000053
Creat 1000 more items cost 0.18599999999969
Creat 1000 more items cost 0.23300000000017
Creat 1000 more items cost 0.28399999999965
Creat 1000 more items cost 0.33100000000013
Creat 1000 more items cost 0.37899999999991
Creat 1000 more items cost 0.4360000000006
Creat 1000 more items cost 0.48399999999947
Creat 1000 more items cost 0.54200000000037
Creat 1000 more items cost 0.58799999999974
Creat 1000 more items cost 0.6279999999997
Creat 1000 more items cost 0.67800000000079
Creat 1000 more items cost 0.75799999999981
Creat 1000 more items cost 0.8119999999999
Creat 1000 more items cost 0.88699999999972
Creat 1000 more items cost 0.96399999999994
Creat 1000 more items cost 1.0450000000001
It's obvious that the script is running slower and slower, I'm wondering what causes it?
dsyrock is online now   Reply With Quote
Old 06-28-2020, 11:21 AM   #2
Amberience
Human being with feelings
 
Amberience's Avatar
 
Join Date: Oct 2006
Location: swing on the spiral of our divinity
Posts: 2,119
Default

IME, logging to the console in Reaper is ridiculously slow. I've had Reaper crash on me outright when logging 1000+ item names or positions in the past, but when I don't log to the console, then the functions are super quick.

Try doing something else other than logging and getting a rough external timing for it using a stopwatch.
__________________
TACOMA NARROWS BRIDGE DISASTER
Full disclosure: I am part of the inMusic Brands BFD team. Views and postings are my own
Amberience is offline   Reply With Quote
Old 06-28-2020, 11:26 AM   #3
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 658
Default

I would guess, that the sheer amount of new items created(10.000) means, that Reaper needs to shuffle around a lot of memory.
The more items, the slower Reaper becomes because of more memory shuffling.

This becomes obvious, if you comment out this line:
Code:
  reaper.Main_OnCommand(41309, 0)  --move item's duplicate
which keeps the script fast.

I would think, that it could be much faster to create the ItemStateChunks you want to have first, add them into the TrackStateChunks of the tracks, in which you want to have the duplicated items and then commit it only once via SetTrackStateChunk.
That way, Reaper would need to shuffle a lot of memory around only once and not 10.000 times.

But it's a wild guess...

@Amberience
The logging isn't the problem in here, as it only happens a few times and always a single line. The console just slows down, if you try to write dozens of lines into it at once for numerous times, which is no the case in dysrock's script.
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1200 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is offline   Reply With Quote
Old 06-28-2020, 11:40 AM   #4
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 295
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
I would think, that it could be much faster to create the ItemStateChunks you want to have first, add them into the TrackStateChunks of the tracks, in which you want to have the duplicated items and then commit it only once via SetTrackStateChunk.
That way, Reaper would need to shuffle a lot of memory around only once and not 10.000 times.
I think TrackStateChunks is the key to make it fast, thanks!
dsyrock is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.