how to avoid script's "slow downs"

Discuss any general programming issues here
Post Reply
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

how to avoid script's "slow downs"

Post by peceha »

Hello,

it has something to do with memory in use. I just noticed that whenever (Hollywood or AMIGA OS3.1) frees some memory the program execution slows down (from average 8 seconds to 12 or even 20 seconds sometimes).
At first I was looking for some mistakes in my script but then I noticed memory amount on the workbench's menu and I'm almost 100% sure that must be the problem.

The program is just an infinite loop (for testing), after every loop it prints out the time how long the calculations took time.
EVERY first run is always the fastest (~8seconds), each other is much slower (~13 seconds or even more, sometime less).
When the program starts I can see that free memory is going down - the loop is finished and the execution time is fine. When the program runs another loop I can see that free memory is going down again but sometimes it stops (not changing) and thet it jumps up (shows more free memory that before) - when that happens my program slows down.

How to avoid this? Or How can I force to free memory when the program is iddle - I tried Garbage collector but no luck.

Thanks.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: how to avoid script's "slow downs"

Post by airsoftsoftwair »

Hard to tell, can you post a minimal example script that shows the problem?
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to avoid script's "slow downs"

Post by peceha »

Sorry for the late reply.
I've been away from amiga for a month already - once I get back to it I'll check couple of ideas that came to my mind and if everything fails then I'll post some examples.
Thank you.
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to avoid script's "slow downs"

Post by peceha »

Hello,
after a few busy days, trying to find out what is causing slow downs (so it will be easy to show on the forum) I came up with something.

IMPORTANT: it should be run on slow computer (like a1200 + blizzard 030)

Code: Select all

@DISPLAY 1,{
	height	= 256,
	width	= 320
}


TextOut(0,0,"part 1")
TextOut(60,0,"part 2")


For Local q=0 To 9
	tbl={}
	
	
	Local color=IIf(q%2=0,#WHITE,#BLACK)
	Local x=1
	
	
	StartTimer(0)
	For Local i=0 To 279
		InsertItem(tbl,i)
		tbl[i]={a={1,0,1,0,0,0,1,0,1}}
		Line(0,10,i*46/279,10,color)
	Next
	TextOut(20,20+q*8,GetTimer(0)\1000)


	ResetTimer(0)
	For i In IPairs(tbl)
		tbl[i].b={}
	
		CreateBrush(i,16,16)
		SelectBrush(i)
			If tbl[i].a[0] Then x=0	Else InsertItem(tbl[i].b,{0,0})
			If tbl[i].a[1] Then x=0	Else InsertItem(tbl[i].b,{1,1})
			If tbl[i].a[2] Then x=0	Else InsertItem(tbl[i].b,{2,2})
			If tbl[i].a[3] Then x=0	Else InsertItem(tbl[i].b,{3,3})
			If tbl[i].a[5] Then x=0	Else InsertItem(tbl[i].b,{5,5})
			If tbl[i].a[6] Then x=0	Else InsertItem(tbl[i].b,{6,6})
			If tbl[i].a[7] Then x=0	Else InsertItem(tbl[i].b,{7,7})
			If tbl[i].a[8] Then x=0	Else InsertItem(tbl[i].b,{8,8})
		EndSelect
		Line(60,10,60+i*46/279,10,color)
	Next
	TextOut(80,20+q*8,GetTimer(0)\1000)
Next


TextOut(#RIGHT,#BOTTOM,"press a key")
WaitKeyDown("any")
There are 2 steps:
- first one is making a table
- second one is making a brushes along with some if's

THE MOST IMPORTANT: slow down occurs when the ELSE-part has something to do with a table
InsertItem(tbl.b,{0,0}) makes it the worst. Below you can se the results from above script (the numbers mean seconds for 1st step and 2nd step):
2 - 5
2 - 5
2 - 13
2 - 11
2 - 5
4 - 10
4 - 9
2 - 15
2 - 14
2 - 13

I run this few times and 5 times in a row I got EXACTLY the same results.


when I change ELSE part a little eg: InsertItem(tbl.b,{0}) - the slow downs are smaller

when I do this: tbl.b=0 , the results look like this:
2 - 3
2 - 3
3 - 3
2 - 4
2 - 3
2 - 3
3 - 3
2 - 3
3 - 3
2 - 3


I'm not a programmer so maybe that behavior is quite normal - if so, then sorry for making problems out of nothing :D
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to avoid script's "slow downs"

Post by peceha »

EDIT to my last post:

that script is doing the same routine 10 times - the listings with the times i gave are showing how many seconds took the first part and the second one. Then the whole procedure is repeated and new times are printed below (up to 10 lines).
Talking about "slow downs" I mean that each repetition should be done within more or less the same time (like the last listing) - in the example above you can see that is not always true.
In my real program that "slow downs" are PITA since the times I'm talking about can grow up to even 50 seconds ( where first run is usually around 11 seconds)
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: how to avoid script's "slow downs"

Post by airsoftsoftwair »

InsertItem() is slow because it always calculates the number of elements in the table in order to check whether the position you passed is invalid. So in performance-critical applications where you only need to append an element to a list, doing this manually will be a lot faster than InsertItem().
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to avoid script's "slow downs"

Post by peceha »

Thanks for the clarification on that command.
I will remove insertItem(), where ever I can and replace it with

Code: Select all

t[listItems(t)]=blabla
If this is "the manual way" :)

Anyway when I changed all insertItem() in the test script above to tbl[listItem(tbl)] it didn't change a thing. The program still runs like before (same results).
And that is something I cannot understand - that script repeats the same procedure 10 times - every time it erases the main (and the only one) table and just does the same things (creates brushes and runs some if's). And every (ok, not every time) it gives different times. As you can see here (each line represents another run, from 1st to 10th):
2 - 5
2 - 5
2 - 13
2 - 11
2 - 5
4 - 10
4 - 9
2 - 15
2 - 14
2 - 13
since everytime the same commands are being executed and the same numbers of time I belive it should give same duration (and I assume that 2sec and 5 sec are the correct ones )

The other strange thing (at least for me) is why every time I run the script (10 repetitions) I get EXACTLY the same results.
Please note I am talking about test done on real a1200 with blizzard 030 (not PC emulator).
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: how to avoid script's "slow downs"

Post by peceha »

Looks like my replacement is not so good since I'm just actually counting items in a table ( same as InsertItem() )
Post Reply