Very long time to execute simple script, why??

Hi,
I write a script for know how many ips has a single MAC. But every iteration take 3-4 seconds in my core.
Why?? I have 3000 macs, and the script take 2 hours


:log info “Start”
#1
:foreach i in=[/ip arp find disabled=no] do={
:local MacNull “00:00:00:00:00:00”
:local Mac “00:00:00:00:00:00”
:local LenMac 0
:local NumIPs 0
:local IP “0.0.0.0”

set Mac [/ip arp get $i mac-address]
set LenMac [:len [$Mac]]

#2
if ( $LenMac > 0 and $Mac != $MacNull) do={
:set NumIPs [/ip arp print count-only where mac-address=$Mac]
#3
if ( $NumIPs > 1) do={
:log warning “MAC:$Mac has $NumIPs IPs”
#3
}
#2
}
#1
}
:log info “Endt”

[/ip arp print count-only where mac-address=$Mac] actually runs through your complete MAC list.
So each iteration actually has 3000 reads through your router’s command interpretor.
If we assume 1 msec each this will give you 3 seconds/iteration.
For a total of 3000 iterations, you will get 2.5 hours.

You can speed this up by loading your MAC list into memory and process them there.

:local arptable "";
:foreach i in=[/ip arp find disabled=no] do={
  :local macaddr [/ip arp get $i mac-address];
  :set $arptable ($arptable, $macaddr);
}

Now process the table as you like:

:for i from 1 to ([:len $arptable] - 1) do={
  :local macaddr [:pick $arptable $i];
  :local ent [:find $arptable $macaddr -1];
  :if ($ent != $i) do={
    :log info "MAC $macaddr found with multiple IPs"
  }
}

Great!!! It works perfectly, and only take 30 seconds VS 2 hours!!
Thank you!!