# [ncl-talk] running sequence of 0s and 1s [SEC=UNCLASSIFIED]

Dennis Shea shea at ucar.edu
Wed Mar 8 08:03:11 MST 2017

```Griff is correct that interpreted languages can be slow executing do loops.
However, I always suggest doing a simple timing test like below. On my MAC,
with, N=10000
gibson_young: N=10000:  ===> 0.03022 seconds

------------------------------------
undef("gibson_young")
function gibson_young(q[*]:numeric)
begin
nq   = dimsizes(q)
qsum = conform(q, 0, -1)   ; initialize to 0

do n=0,nq-1
if (q(n).eq.0) then
t = 0
end if

if (q(n).eq.1) then
qsum(n) = t
t       = t + 1
end if
end do

return(qsum)
end
;=====================================
;                  MAIN
;=====================================
;
;   desired result: 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0
q = (/0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0/)
qsum = gibson_young(q)
print(q+"   "+qsum)

;========

N  = 10000
x  = random_uniform(-10,10,N)
x  = where(x.lt.0, 0, 1)

tStrt  = get_cpu_time()
xsum   = gibson_young(x)
print("gibson_young: N="+N+ ": " + (get_cpu_time() - tStrt))

;print(xsum)

On Tue, Mar 7, 2017 at 9:43 PM, Dennis Shea <shea at ucar.edu> wrote:

> Never underestimate Aussie brute force!
>
>
> ==========
> undef("gibson_young")
> function gibson_young(q[*]:numeric)
> local nq, qsum, n, t
> begin
>   nq   = dimsizes(q)
>   qsum = conform(q, 0, -1)   ; initialize to 0
>
>   do n=0,nq-1
>     if (q(n).eq.0) then
>         t = 0
>     end if
>
>     if (q(n).eq.1) then
>         qsum(n) = t
>         t       = t + 1
>     end if
>   end do
>
>   return(qsum)
> end
> ;
> ;   desired result: 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0
>   q = (/0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0/)
>   qsum = gibson_young(q)
>   print(q+"   "+qsum)
>
> ===
>
>
>
> (0)    0   0
> (1)    0   0
> (2)    0   0
> (3)    1   0
> (4)    1   1
> (5)    0   0
> (6)    1   0
> (7)    1   1
> (8)    1   2
> (9)    1   3
> (10)    1   4
> (11)    0   0
> (12)    1   0
> (13)    1   1
> (14)    1   2
>
>
> On Tue, Mar 7, 2017 at 9:19 PM, Griffith Young <griffith.young at bom.gov.au>
> wrote:
>
>> Hello Peter,
>>
>>
>>
>> Not if you go to the casino and are doubling up your bet.
>>
>>
>>
>> This code seems to work...
>>
>>
>>
>> v = {vector or array of 1's and 0's}
>>
>> c = v
>>
>> t = 0
>>
>> do i = 0, dimsizes(v) - 1
>>
>>     if v(i) .eq. 0 then
>>
>>         c(i) = 0
>>
>>         t = 0
>>
>>     end if
>>
>>     if (v(i) .eq. 1) then
>>
>>         c(i) = t
>>
>>         t = t + 1
>>
>>     end if
>>
>> end do
>>
>>
>>
>> Caveat: "Since NCL is an interpreted language, it is best to avoid do
>> loops as much as possible. They can cause considerable slow downs. Small
>> loops should not be a problem."
>>
>>
>>
>> Regards, Griff.
>>
>>
>>
>>
>>
>> *From:* ncl-talk-bounces at ucar.edu [mailto:ncl-talk-bounces at ucar.edu] *On
>> Behalf Of *Dennis Shea
>> *Sent:* Wednesday, 8 March 2017 2:59 PM
>> *To:* Peter Gibson
>> *Cc:* ncl-talk at ucar.edu
>> *Subject:* Re: [ncl-talk] running sequence of 0s and 1s
>>
>>
>>
>> Sorry, no. Pretty specialized function ...
>>
>> : 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 ...  input: 1st 1 is a flag
>>
>> : 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0
>>
>>
>>
>> On Tue, Mar 7, 2017 at 8:17 PM, Peter Gibson <peter.gibson at unsw.edu.au>
>> wrote:
>>
>> Hello,
>>
>> Is there a function to calculate the running length of 0/1s in a sequence
>> in NCL?
>>
>> for example if I had a vector     v : 0 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 ...
>> the running sequence would be   : 0 0 0 0 1 0 0 1 2 3 4 0 0 1 2 0 ...
>>
>> I see the function dim_numrun counts the number of unique sequence
>> lengths which is similar but not exactly what I am after ....
>>
>>
>>
>> Thanks,
>>
>> Peter
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170308/8418656b/attachment.html
```