Don't have an account? Register now to chat, post, use our tools, and much more.
Latest Headlines
Online Users
There are 130 users online: 6 members, 99 guests and 25 bots. Members: HOMER-16, Piguy-3.14, Tari. Bots: Spinn3r (1), Magpie Crawler (6), VoilaBot (1), Googlebot (17).
RSS & Social Media
SAX
You must log in to view the SAX chat widget
|
| Author |
Message |
|
MufinMcFlufin

Advanced Member

Joined: 29 Aug 2009 Posts: 252 Location: C:\Users\Admin\Desktop
|
Posted: 15 Oct 2010 11:03:03 pm Post subject: Nested Loops in One Line of Code |
|
|
Well, after thinking a bit about a piece of code made by Weregoose, I wanted to try and replace this code:
(Note, that this code is for a program I made that finds the most similar printable character to one that you draw 8 pixels to the right of the tested characters. I don't think much any further explanation is needed, but do ask if you need more info)
Code: :For(D,1,6
:For(O,2,6
:L1(A)+(pxl-Test(D,O)=pxl-Test(D,O+8→L1(A
:End
:End
I wanted to replace that code with some more efficient code that would use something along the lines of the summation of the sequence of pxl-tests, or whatnot. Then I decided to try and nest one seq( into another, and see if that would work. Here's what I came up with, after not doing my research on the seq( command:
Code: :sum(seq(sum(seq(pxl-Test(D,O)=pxl-Test(D,O+8),O,2,6)),D,1,6
Well...that didn't work, as apparently you can't nest one seq( into another.
So, after some fiddling around, I came up with this code:
Code: :round(FnInt(sum(seq(pxl-Test(int(D),int(O))=pxl-Test(int(D),int(O)+8),O,2,7-{EE}‾9)),D,1,7-{EE}‾9),0
Toyed around with it, and found it works perfectly, however I've found that it's speed depends on how many pixels are the same. What I know so far about it is that it runs significantly faster with 30 pixels the same (all pixels the same in it's testing area) than with 29 pixels the same. I made a little program to test and record the speed of this code comparing all situations, from 30 pixels the same, to 0 pixels the same. I'm going to have it run throughout the night (I put it on wabbit emu, so I won't have to worry about battery life), as it's so far been running about 10 minutes, and it's still at 1 changed pixel. It currently does only 50 iterations per pixel change considering the fastest I've seen it do so far is about 3-5 seconds with 30 pixels the same, against about 30 seconds to a minute with 29 pixels the same, so 4000 iterations didn't seem necessary. Here's the code I'm using for it:
Code: :ClrDraw
:Pt-Off(0,0
:50→C
:{6,6→dim([A]
:Fill(0,[A]
:For(E,1,6
:For(P,2,6
:startTmr→B
:For(A,1,C
:round(FnInt(sum(seq(pxl-Test(int(D),int(O))=pxl-Test(int(D),int(O)+8),O,2,7-{EE}‾9)),D,1,7-{EE}‾9),0
:End
:checkTmr(B)/C→[A](E,P
:Disp Ans
:Pxl-Change(E,P
:End
:End
Here are the results I got: Code: # Pixels different: 0 1 2 3 4 5 6 7 8 9 10 11 12
Time per iteration: 1.4 42.82 45.58 48.32 48.28 50.82 89.74 78.58 78.54 89.56 4.12 42.8 45.54
# Pixels different: 13 14 15 16 17 18 19 20 21 22 23 24
Time per iteration: 48.3 48.26 50.78 89.68 78.56 78.5 89.52 50.74 47.96 47.94 45.14 42.4
Anyone have a clue as to why the time varies so much depending on how many pixels are different? _________________
MufinMcFlufin's quote of the week:
"Diapers and Politicians should both be changed often for the same reason."
-Anonymous |
|
| Back to top |
|
|
KermMartian

Site Admin

Joined: 14 Mar 2005 Posts: 55879 Location: Earth, Sol, Milky Way
|
Posted: 17 Oct 2010 07:03:51 pm Post subject: |
|
|
Absolutely. It hinges on how the calculator's integration works. If there's an area with high delta-slope, it will use a much more fine-grained X increment in that area to increase accuracy. In areas with low or zero delta-slope, it will use a larger increment to save time on the assumption that not much is changing. _________________
 |
|
| Back to top |
|
|
MufinMcFlufin

Advanced Member

Joined: 29 Aug 2009 Posts: 252 Location: C:\Users\Admin\Desktop
|
|
| Back to top |
|
|
|
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
© Copyright 2000-2013 Cemetech & Kerm Martian :: Page Execution Time: 0.027699 seconds.
|