Compare a number with list of numbers?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Compare a number with list of numbers?

Post by Otrebor »

Hi
I need to do this comparison as fast as possible.
Since the elements in the list vary in quantity (even empty), i thought in NewList() and NewMap().

Code: Select all

NewList BRKPT.u()

AddElement(BRKPT())
BRKPT() = 100
AddElement(BRKPT())
BRKPT() = 200
AddElement(BRKPT())
BRKPT() = 300
AddElement(BRKPT())
BRKPT() = 400
AddElement(BRKPT())
BRKPT() = 500
AddElement(BRKPT())
BRKPT() = 600
AddElement(BRKPT())
BRKPT() = 700
AddElement(BRKPT())
BRKPT() = 800

time= ElapsedMilliseconds()

For cycle = 0 To 1000
  For pc = 0 To 65535
    ForEach BRKPT()
      If BRKPT() = pc
        ;call procedure
      EndIf
    Next
  Next pc
Next cycle

MessageRequester("Time total",Str(ElapsedMilliseconds() - time))

Code: Select all

NewMap BRKPT.u()

BRKPT("100") = 100
BRKPT("200") = 200
BRKPT("300") = 300
BRKPT("400") = 400
BRKPT("500") = 500
BRKPT("600") = 600
BRKPT("700") = 700
BRKPT("800") = 800

time= ElapsedMilliseconds()

For cycle = 0 To 1000
  For pc = 0 To 65535
    If FindMapElement(BRKPT(), Str(pc))
      ;call procedure
    EndIf
  Next pc
Next cycle

MessageRequester("Time total",Str(ElapsedMilliseconds() - time))
In my computer NewList() is little better (the opposite of what i thought).
Is there any other way more fast?
thanks
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Compare a number with list of numbers?

Post by infratec »

For this case an array will be faster then a list.
But it is less flexible in adding / deleting elements.

The Map is slower because of the needed string functions.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4661
Joined: Sun Apr 12, 2009 6:27 am

Re: Compare a number with list of numbers?

Post by RASHAD »

You can use Google Tech.for search :D (Of course that is not true)
That is if you insist to use List() instead of Array()
-Create more than one List() each one for zone of numbers
-When your pc variable is equal to a zone just Search that zone only(The corresponded List())
Egypt my love
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Compare a number with list of numbers?

Post by Otrebor »

Thank you both!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Compare a number with list of numbers?

Post by wilbert »

Otrebor wrote: Sat Aug 07, 2021 9:02 pmI need to do this comparison as fast as possible.
Since the elements in the list vary in quantity (even empty), i thought in NewList() and NewMap().
I don't know if the code you posted has anything to do with how you want to use it.
If I look at the code you posted, it looks like you are using it as 65536 flags and you want to check which flags have been set.
If that would be the case, there are better and faster ways to do this.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
STARGÅTE
Addict
Addict
Posts: 2085
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Compare a number with list of numbers?

Post by STARGÅTE »

I didn't understand the code as well. You have a list of numbers BRKPT(), but you have also a loop with (all) numbers For pc = 0 To 65535.
And then you call a procedure, when BRKPT() = pc. Why you do not just loop through BRKPT() and call all numbers?
Because in fact, you call all numbers, just in order. So a SortList can help.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Compare a number with list of numbers?

Post by Otrebor »

@wilbert @STARGATE

My example does not fully identify with my needs. The variable 'cycle' used here is just to give me some time measurement, when comparing Newlist() with Newmap().

In fact i'm wondering if i will be able to implement in my debugger in my z80 code, the possibility to choose more than an single address to compare with pc (program counter). In my current version i can set just a single address in BRKPT and, after every z80 instruction exectuted, compare if BRKPT = pc to then call my debugger procedure.

Unfortunately my tests shows me drop down in performance, even using Newlist().
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Compare a number with list of numbers?

Post by wilbert »

What you describe is just a flag for every possible program counter address.
Using a 8 KiB bit array (65536 bits) with a set bit, clear bit and test bit procedure is all you need.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Compare a number with list of numbers?

Post by mk-soft »

I had the same thing as wilbert.
But for the small amount of data, a simple array, "Dim BRKPT.b(65535), and you just take the address pointer as the index on the array.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 201
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Compare a number with list of numbers?

Post by Otrebor »

Thank you wilbert and mk-soft!

Code: Select all

Dim BRKPT.b(65535)

;Example:
;set breakpoints at some address
BRKPT(16384)=1
BRKPT(32768)=1

For pc = 0 To 65535
  If BRKPT(pc)=1
    ;call procedure
  EndIf
Next pc
is this what you mean? Seems more simple than i thought... :oops:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Compare a number with list of numbers?

Post by wilbert »

Otrebor wrote: Sun Aug 08, 2021 2:13 pmis this what you mean? Seems more simple than i thought... :oops:
Yes, that's both the easiest and fastest way. :)
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Compare a number with list of numbers?

Post by mk-soft »

Exactly,

Quite simple and quick
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply