amiga.org
     
iconAll times are GMT -6. The time now is 04:05 PM. | Welcome to Forum, please register to access all of our features.

» Amiga.org » Operating System Specific Discussions » Other Operating Systems » Programming question! :)

Other Operating Systems This forum is to allow our members to discuss other (non-Amiga-related) operating systems.

Reply
 
Thread Tools Display Modes
Old 04-16-2012, 03:08 PM   #16
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Duce View Post
Threads like this are what makes A.org great. People helping people out.
There are loads of great programmer on A.org... Odd actually that there isn't a dedicated forum for programming issues?
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Old 04-16-2012, 03:23 PM   #17
Zac67
Kindred of Babble-on
Points: 12,421, Level: 72 Points: 12,421, Level: 72 Points: 12,421, Level: 72
Activity: 15% Activity: 15% Activity: 15%
 
Zac67's Avatar
 
Join Date: Nov 2004
Location: Erlangen, Germany
Posts: 2,851
Blog Entries: 4
Default Re: Programming question! :)

Actually, comparing against an array uses less RAM all in all (just the values and the loop, you save the space of all but one cmp and blt ops minus loop ops). On a somewhat caching CPU it should also perform better since the loop needs only be fetched once and then just the array values get read - the slowest is always the memory. Without cache your approach will be faster, saving the loop overhead.

Also depends on the indexing range the CPU is capable of, of course...

You could also combine both methods with a larger loop (did that once on a 6502 to save the few cycles I needed).
Zac67 is offline   Reply With Quote
Old 04-16-2012, 03:53 PM   #18
Jose
Kindred of Babble-on
Points: 21,917, Level: 92 Points: 21,917, Level: 92 Points: 21,917, Level: 92
Activity: 10% Activity: 10% Activity: 10%
 
Join Date: Feb 2002
Posts: 2,786
Default Re: Programming question! :)

I have an idea. Depending on how you want to optimize it (apparently for speed rather than size) you could organize ranks comparisons in a binary tree like sequence (assuming the ranks are fixed). This would grant you the fastest search results possible but probably a tid bit bigger code.
Don't ask me for a code sample right now though
__________________
\"We made Amiga, they {bleep}ed it up\"
Jose is offline   Reply With Quote
Old 04-16-2012, 03:58 PM   #19
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Zac67 View Post
Actually, comparing against an array uses less RAM all in all (just the values and the loop, you save the space of all but one cmp and blt ops minus loop ops). On a somewhat caching CPU it should also perform better since the loop needs only be fetched once and then just the array values get read - the slowest is always the memory. Without cache your approach will be faster, saving the loop overhead.

Also depends on the indexing range the CPU is capable of, of course...

You could also combine both methods with a larger loop (did that once on a 6502 to save the few cycles I needed).
The code needs to execute as quickly as possible, I have very few CPU cycles and no cache to speed the loop (though the loop is where my instincts took me).

Trying to do real time stuff with 8bit Microcontrollers is fun!

-edit- just want to add that the uc I'm using has plenty of program memory 32k, but only 2k of ram... So I need to be a bit careful with my arrays and variables
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8

Last edited by bloodline; 04-16-2012 at 04:03 PM..
bloodline is online now   Reply With Quote
Old 04-16-2012, 03:59 PM   #20
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Jose View Post
I have an idea. Depending on how you want to optimize it (apparently for speed rather than size) you could organize ranks comparisons in a binary tree like sequence (assuming the ranks are fixed). This would grant you the fastest search results possible but probably a tid bit bigger code.
Don't ask me for a code sample right now though
Not sure a binary tree would be faster, post some code and we can look through the merits
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Old 04-16-2012, 05:26 PM   #21
Leffmann
Hobbyist
Points: 1,786, Level: 24 Points: 1,786, Level: 24 Points: 1,786, Level: 24
Activity: 8% Activity: 8% Activity: 8%
 
Join Date: Feb 2011
Posts: 56
Default Re: Programming question! :)

Don't know about a binary tree, but a binary search will reduce the number of searches to log2 n, i.e. if you have an array of 256 numbers it will do 8 searches. Basic implementation would be:

Code:
// For an array of 100 numbers, call with binsearch(n, 0, 99);

int binsearch(int n, int a, int b)
{
  if(a == b)
    return a;

  int mid = a + (b-a)/2;
  
  if(n < array[mid])
    return binsearch(n, a, mid);
  else
    return binsearch(n, mid+1, b);
}
EDIT: this binary search assumes the array is sorted in ascending order.

Last edited by Leffmann; 04-16-2012 at 05:30 PM..
Leffmann is offline   Reply With Quote
Old 04-16-2012, 07:53 PM   #22
smerf
Defender of the Faith
Points: 11,436, Level: 70 Points: 11,436, Level: 70 Points: 11,436, Level: 70
Activity: 2% Activity: 2% Activity: 2%
 
smerf's Avatar
 
Join Date: Mar 2002
Location: Pennsyltucky, USA
Posts: 1,490
Blog Entries: 1
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
I have a list that I want to compare a value against to determine the value's "rank", the problem is that once I have determined the rank I wish to end the search... In ASM this is easy (please bare with my half remembered 68k):
Code:
_start  move.l _number,d0
          cmpi.l #4185,d0
          blt.s _case4184
          cmpi.l #4507,d0
          blt.s _case4507
          cmpi.l #4883,d0
          blt.s _case4883
          cmpi.l #5327,d0
          blt.s _case5327
          move.l 0,d0
_break

           rts

_case4185
           move.l #4,d0
           jmp _break

_case4507
           move.l #3,d0
           jmp _break

_case4883
           move.l #2,d0
           jmp _break

_case5327
           move.l #1,d0
           jmp _break
But I want to write this in C... Suffice to say the actual list is much larger and CPU time is at a premium, is there any way I can write it without using Goto?
Hi,

This is quite simple, just go to kings Knight pawn, and move two to the left, not forgetting to move queen pawn two spaces to the right, and then you get
checkmate.

If you figured out that I don't have a clue to what your talking about, it means that you are 10 times smarter than the average person here at Amiga.org

:-)

smerf
__________________
I have no idea what your talking about, so here is a doggy with a small pancake on his head.

MorphOS is a MAC done a little better
smerf is offline   Reply With Quote
Old 04-17-2012, 08:19 AM   #23
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Leffmann View Post
Don't know about a binary tree, but a binary search will reduce the number of searches to log2 n, i.e. if you have an array of 256 numbers it will do 8 searches. Basic implementation would be:

Code:
// For an array of 100 numbers, call with binsearch(n, 0, 99);

int binsearch(int n, int a, int b)
{
  if(a == b)
    return a;

  int mid = a + (b-a)/2;
  
  if(n < array[mid])
    return binsearch(n, a, mid);
  else
    return binsearch(n, mid+1, b);
}
EDIT: this binary search assumes the array is sorted in ascending order.
An elegant search algorithm, but unsuitable for my application as I'm not searching for a specific value, but a boundary
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Old 04-17-2012, 08:21 AM   #24
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Hmmm, looking around the interwebs suggest that using Staf's ternary operator based code might compile to more efficient code... I'll have to implement both and look at the Asm
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Old 04-17-2012, 08:55 AM   #25
Jose
Kindred of Babble-on
Points: 21,917, Level: 92 Points: 21,917, Level: 92 Points: 21,917, Level: 92
Activity: 10% Activity: 10% Activity: 10%
 
Join Date: Feb 2002
Posts: 2,786
Default Re: Programming question! :)

Is the size between boundaries constant ? (i.e. is the total nr. of ranks a multiple of the size of one rank).
__________________
\"We made Amiga, they {bleep}ed it up\"
Jose is offline   Reply With Quote
Old 04-17-2012, 08:58 AM   #26
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Jose View Post
Is the size between boundaries constant ? (i.e. is the total nr. of ranks a multiple of the size of one rank).
No, for the sake of argument the boundaries are somewhat arbitrary... In reality they should scale geometrically in most cases (but not all, due to mechanical requirements).

-edit-
The values I used in my example were real, and if you were to plot them on a graph you should see the beginnings of a curve, here is more of the sequence:

5859
6525
7342
8371
9766
11719
14648
19531
29297

You get the idea
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8

Last edited by bloodline; 04-17-2012 at 09:13 AM..
bloodline is online now   Reply With Quote
Old 04-17-2012, 09:33 AM   #27
Trev
Slacker
Points: 11,230, Level: 69 Points: 11,230, Level: 69 Points: 11,230, Level: 69
Activity: 2% Activity: 2% Activity: 2%
 
Trev's Avatar
 
Join Date: May 2003
Location: San Francisco, California, US
Posts: 1,514
Default Re: Programming question! :)

If the compiler is at all recent, I'd expect the trigraph and the function (inlined) to produce the same code. Use the function. :-) (Or put the trigraph in a function.)

I know this isn't Amiga coding, but as an aside, I really miss utilitybase.com. I lost interest in my Amigas when it shut down....
__________________
Sam440ep-flex 733 MHz/1 GB RAM/Radeon 9250/AmigaOS4.1 Update 2
borked A1200/Blizzard1260+SCSI-IV/Z4+MediatorZIV/Deneb/Voodoo3/CatweaselMk3
more borked A1200/MBX1200z/Indivision
A500/clockport/RRNet
A600/A603
Power Mac G4 QuickSilver/MorphOS 2.6
Trev is offline   Reply With Quote
Old 04-17-2012, 10:10 AM   #28
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Trev View Post
If the compiler is at all recent, I'd expect the trigraph and the function (inlined) to produce the same code. Use the function. :-) (Or put the trigraph in a function.)

I know this isn't Amiga coding, but as an aside, I really miss utilitybase.com. I lost interest in my Amigas when it shut down....
This is slightly related to Amiga programming... Since much of coding for tr Amiga was trying to squeeze every last drop of performance from decidedly weak CPUs... At least it is now

Also many people on this site have expressed an interest in learning to program, they would be wise to read threads like these
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Old 04-17-2012, 10:47 AM   #29
Leffmann
Hobbyist
Points: 1,786, Level: 24 Points: 1,786, Level: 24 Points: 1,786, Level: 24
Activity: 8% Activity: 8% Activity: 8%
 
Join Date: Feb 2011
Posts: 56
Default Re: Programming question! :)

Quote:
Originally Posted by bloodline View Post
An elegant search algorithm, but unsuitable for my application as I'm not searching for a specific value, but a boundary
Actually it's adjusted to do exactly what you want, and can even be optimized further.
Leffmann is offline   Reply With Quote
Old 04-17-2012, 11:31 AM   #30
bloodline
Master Sock Abuser
Points: 37,111, Level: 100 Points: 37,111, Level: 100 Points: 37,111, Level: 100
Activity: 10% Activity: 10% Activity: 10%
 
bloodline's Avatar
 
Join Date: Mar 2002
Location: London, UK
Posts: 11,656
Blog Entries: 3
Default Re: Programming question! :)

Quote:
Originally Posted by Leffmann View Post
Actually it's adjusted to do exactly what you want, and can even be optimized further.
Appologies! I only quickly looked over it If I had a larger array I would certainly investigate this method... I'm intrigued to see how a tiny microcontroller would cope with this kind of search!
__________________
My iPhone Game: Puny Humans -
http://itunes.apple.com/gb/app/puny-...362230281?mt=8
bloodline is online now   Reply With Quote
Reply

Bookmarks

Tags
programming , question

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