LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

find the counts of consecutive 0s and 1s

Solved!
Go to solution

I ran a benchmark on my code vs Crossrulz' code. It resulted in my code being about 30% faster. Crossrulz will also go slower if your array input gets bigger because there is more logic within each iteration.

Ones and Zeros VS.png

 

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 11 of 38
(1,501 Views)

@James.M wrote:

ZerosAndOnes.png


1. Instead of the rotate, I think I would just use a Delete From Array to eliminate the first element.

2. Your code only really works if the first element and last element are different.  The Delete From Array part should fix that, somewhat.  Still need to think about the final element somehow.

3. Instead of subtracting, just use Equals.  That will also eliminate the need for the Not Equal To 0 node.

 


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 12 of 38
(1,489 Views)

OOps misread the post

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 13 of 38
(1,470 Views)

@crossrulz wrote:

1. Instead of the rotate, I think I would just use a Delete From Array to eliminate the first element.

2. Your code only really works if the first element and last element are different.  The Delete From Array part should fix that, somewhat.  Still need to think about the final element somehow.

3. Instead of subtracting, just use Equals.  That will also eliminate the need for the Not Equal To 0 node.


Ooo good catch. Using a comparison instead of the Subtract sped it up by a little bit.

I'm trying to get the logic to work when the first and lest index don't match without adding too much processing.

 

While doing this, I noticed yours has a problem too. If you add a single "1" to the end of the array, yours adds another element to your Zero output instead of your One output. If you add two 1s, it works just fine.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 14 of 38
(1,445 Views)

James.Morris wrote:

While doing this, I noticed yours has a problem too. If you add a single "1" to the end of the array, yours adds another element to your Zero output instead of your One output. If you add two 1s, it works just fine.


Yep, I wire the output tunnel to the wrong side of the Feedback Node.  Oh well.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 15 of 38
(1,437 Views)

Yup, that fixed it.

 

Anyways, I've spent way too much time here. walidbalid, you can figure it out from here. Crossrulz code gives you the end results accurately. If it is really going take a long time, you can give my code a shot, but just make sure your input array has opposing first and last values.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


Message 16 of 38
(1,420 Views)

How fast does this conditional accumulation work? It is not regular autoindexing (for loop does not know end size), so will you end up with build array in a loop with indefinite size? Or is it equal to initialize full array, then delete remaining portion?

 

0 Kudos
Message 17 of 38
(1,394 Views)

@Alexander_Sobolev wrote:

How fast does this conditional accumulation work? It is not regular autoindexing (for loop does not know end size), so will you end up with build array in a loop with indefinite size? Or is it equal to initialize full array, then delete remaining portion?


The autoindexing FOR loop allocates the maximum sized array based on how many times the loop could run.  When using conditional, it just throws away what was not needed once the FOR loop is complete.  It is VERY effecient.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 18 of 38
(1,381 Views)

I haven't timed anyone else's code (could someone post the times?), but decided that I should "enter the race".  Since the length of the strings is given as "very long", I generated 10 million arrays of length between 1 and 10 (uniformly distributed), alternating between True and False (I used Booleans instead of Ones and Zeros).  This took about 4 seconds,  I then created a single array that counted the sizes of the "runs" (not surprising, but the size of this array was 10 million, and its sum was the size of the Big Boolean Array).  Doing this took about 0.48 seconds.  Splitting this array into Trues and Falses added about 0.14 seconds to the time.

 

I also tried it for 100 million "runs", but LabVIEW gave me an Out of Memory error.  I could go up to 30 million, and it took three times longer (at least it scales linearly!).

 

Before I post it, I'm going to try to time it against some of the methods posted, and won't bother if I finish dead last ...

 

Bob Schor

0 Kudos
Message 19 of 38
(1,373 Views)

Bob_Schor wrote:

Before I post it, I'm going to try to time it against some of the methods posted, and won't bother if I finish dead last ...


Don't take that attitude.  This is not a competition.  Go ahead and post what you got.  It might give somebody an idea and then everything will blow wide open.  Either that or we learn something as we poke big giant holes in it.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 20 of 38
(1,364 Views)