Page 2 of 3 <123>
Topic Options
#106508 - 2003-11-03 04:32 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
yip - you got it ... I can see where the guys went astray though ... when I mention a number like &00DDEE i am just using the & to denote a hex number in kix, kinda like saying 0x00DDEE in other langauges - ultimately its all decimal ... got yah now.
Top
#106509 - 2003-11-03 04:38 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
code:
function FadeToBlack($color, $level)

Dim $B, $G, $R
$B=$color/65536
$G=($color mod 65536)/256
$R=$color-($B*65536)-($G*256)


$B = IIF($B>$level, $level, $B)
$G = IIF($G>$level, $level, $G)
$R = IIF($R>$level, $level, $R)

;by matching level these checks are no longer required.
;$B = IIF($B<0, 0, $B)
;$G = IIF($G<0, 0, $G)
;$R = IIF($R<0, 0, $R)

$FadeToBlack = ($B*65536)+($G*256)+$R

endfunction



[ 03. November 2003, 16:41: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106510 - 2003-11-03 04:41 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Howard - PERFECT !

Jlo - you got another submission ? Want to do speed tests now.

Top
#106511 - 2003-11-03 04:54 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Shawn did you catch the edit above?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106512 - 2003-11-03 04:59 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
yes I did, thank you. plus - I took the liberty of renaming all your internal $vars to single letters, to make it afap.
Top
#106513 - 2003-11-03 05:04 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Like this?
code:
function FadeToBlack($c, $)

Dim $B, $G, $R
$B=$c/65536
$G=($c mod 65536)/256
$R=$c-($B*65536)-($G*256)


$B = IIF($B>$, $, $B)
$G = IIF($G>$, $, $G)
$R = IIF($R>$, $, $R)

$FadeToBlack = $B*65536+$G*256+$R

endfunction



[ 03. November 2003, 17:06: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106514 - 2003-11-03 05:11 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Shawn, I would think that this would be faster. The IIF has to rest the value of the color variable even if it did not need to change it.
code:
function FadeToBlack($c, $)

Dim $B, $G, $R
$B=$c/65536
$G=($c mod 65536)/256
$R=$c-($B*65536)-($G*256)

if $B>$ $B=$ endif
if $G>$ $G=$ endif
if $R>$ $R=$ endif
$FadeToBlack = $B*65536+$G*256+$R

endfunction

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106515 - 2003-11-03 05:45 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Howard, just working on my short game here. Wondering if this might be shorter/faster ...

code:
$r = $c & &FF
$g = ($c & &FF00) / 256
$b = ($c & &FF0000) / 65536

plus, I concatenated your IIF's (which I like) into a big long expression:

code:
$FadeToBlack = IIF($B>$, $, $B) * 65536 + IIF($G>$, $, $G) * 256 + IIF($R>$, $, $R)

-Shawn

[ 03. November 2003, 17:48: Message edited by: Shawn ]

Top
#106516 - 2003-11-03 06:16 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
hmmm, combining both strategies yields:

code:
function FadeToBlack($c, $)

dim $b, $g, $r

$b = $c / 65536
$g = ($c & &FF00) / 256
$r = $c & &FF

$FadeToBlack = iif($b>$, $, $b) * 65536 + iif($g>$, $, $g) * 256 + IIF($r>$, $, $r)

endfunction


Top
#106517 - 2003-11-03 06:29 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Think this is actually four chars shorter. Even with the redundancy ...

code:
function FadeToBlack($c, $)

$FadeToBlack =
iif($c/65536>$, $, $c/65536) * 65536 +
iif(($c & 65280) / 256>$, $, ($c & 65280) / 256) * 256 +
iif($c & 255>$, $, $c & 255)

endfunction



[ 03. November 2003, 18:29: Message edited by: Shawn ]

Top
#106518 - 2003-11-03 06:36 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
But is your goal the least characters used or the most efficient code with he fastest execution?

Do you have any benchmarks for each version?

[ 03. November 2003, 18:45: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106519 - 2003-11-03 06:41 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
yeah, i'm just playing around with the solution now - trying to get the smallest solution. Will end-up using the fastest solution at the end of the day. Will be fun to compare all the solutions.
Top
#106520 - 2003-11-03 06:47 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
well, here are some benchmarks

the last snippet posted (the scrunched IIF's) for 10,000 iterations takes (avg) 2293 msecs.

the second-last snippet posted (the combined solution with BGR and IIF's seperate) takes (avg) 2874 msecs for 10,000 iterations.

So it would seem that smaller and scrunched helps with the numbers. Should try the IF-ENDIF solution for speed though.

[ 03. November 2003, 18:49: Message edited by: Shawn ]

Top
#106521 - 2003-11-03 06:50 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Yes, what is that result???
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106522 - 2003-11-03 06:52 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You last IF/ENDIF solution for 10,000 iterations takes (avg) 3615 msecs.
Top
#106523 - 2003-11-03 06:56 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Crap! [Frown]

I still think the If...Endif should be faster than IIF.

Keep the If..Endif's and toss the "mod" operator in favor of the "&" bitwise operator. The slowness is probably in the additional math I do.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106524 - 2003-11-03 06:59 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
code:
function FadeToBlack($c, $)

Dim $B, $G, $R
$B=$c/65536
$G=($c & &FF00) / 256
$R=$c & &FF

if $B>$ $B=$ endif
if $G>$ $G=$ endif
if $R>$ $R=$ endif
$FadeToBlack = $B*65536+$G*256+$R

endfunction

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106525 - 2003-11-03 07:02 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Runs about 3275 msecs on my machine, so the math seems to have an impact. Here is the benchmark script, just rename the function as required. Don't think the order of functions has an impact:

code:
break on

$start = @TICKS

for $i = 0 to 10000
$c = fadeToBlack2(&FFAADD,&DD)
next

?"elapsed=" @TICKS-$start

exit 1

function FadeToBlack($c, $)

dim $B, $G, $R

$b = $c / 65536
$g = ($c & &FF00) / 256
$r = $c & &FF

if $B>$ $B=$ endif
if $G>$ $G=$ endif
if $R>$ $R=$ endif

$FadeToBlack = $B*65536+$G*256+$R

endfunction

function FadeToBlack2($c, $)

$FadeToBlack2 =
iif($c/65536>$, $, $c/65536) * 65536 +
iif(($c & 65280) / 256>$, $, ($c & 65280) / 256) * 256 +
iif($c & 255>$, $, $c & 255)

endfunction


Top
#106526 - 2003-11-03 07:25 PM Re: Mini-putt for a slow day on korg ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Shawn, after some experimentation. I have concluded that the high overhead operation is the instantiation of variables. Your FadeToBlack2 avoids the creation of variables as much as seems possible. The additional calculations appear to add less burden to the process than the creation of variables. Good job optimizing.

[ 03. November 2003, 19:28: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#106527 - 2003-11-03 07:34 PM Re: Mini-putt for a slow day on korg ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
The other thing that may contribute to the greater speed of IIF is the following (just speculating here). Even though the IIF's have redundant calculations, the "checking" and the "result" are internalized within the IIF function itself.

Unlike the IF/ENDIF code - these statements have to be "parsed-around" by the interpreter (even though it is supposed to be byte-code internally) - yeah, maybe ?

Top
Page 2 of 3 <123>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 533 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.071 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org