From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

in conditional structure, how do I set what it tested first?

The compiler team could do this, there is risk anytime you mess around with code that is as fundamental as the case structure, which I believe is in the top 2 or 3 most commonly used nodes.  That risk had better have a big reward.

 

In parallel computing there is the well-known Amdahl's law which relates the expected speedup to the fraction of code that is parallelized.  I can make up a similar function for the expected speed-up for optimizing a fraction of the code.  If you speed up a fraction f of your code by a factor of N, then the overall improvement Neff is 1/[1 + f*(1-N)/N].  That means if half of your code is spent doing the case structure comparisons (a very, very optimistic guess), and you improve it by a factor of 2, your effective improvement is only 4/3.  In reality, based on the speediness of the operation (see the slopes on the graph), it is highly doubtful that many programs spend a significant fraction of their time doing the actual case structure comparisons.  Therefore it would seem the return is minimal and hard to justify the risk.

 

Message 11 of 20
(1,899 Views)

I didn't know about that potential nesting optimization but I would be interested if that scaled beyond 2 nested structures.

Matt J | National Instruments | CLA
Message 12 of 20
(1,893 Views)

It's time for you to take a step back and come at this from a rational perspective.

 

You're concerned because your bit of code takes ~6s to run.

 

You're focusing on case structure selection.  In a best case example, you're shown there's a potential difference of 2.5ms.

 

For context, you'd need 400 similar case structures to optimize this bit of code by 1s (which really, won't get you to where you want to be if you're looking to run 100k+ iterations).

 

You're doing something very common in software engineering.  You've got a theory and you're diving in.  You won't be satisfied until you've figured out the BEST way to optimize this part of your program.  You can spend countless hours focusing on this and in the end, you'll get to the conclusion you should already be at (this is unlikely to be the bulk of your 6s).  Someone else mentioned it wasn't generally a good idea to optimize as you go.  This is a key component to that argument.  

 

This is where you want to take a deep breath and step back.  Instead of focusing on this theory, focus on splitting up your program a bit with reasonable benchmarks to try to determine where the 6s are being spent.  If you've already done this and you've found the case structure to be the issue, it's more likely the code contained within the case structure is the actual issue.  

 

You've been asked to share the code several times and ignored it.  That likely means you're unable/unwilling to share the code.  That's fine.  But, it means you should be taking the time to benchmark the various sections of the code yourself to determine where the actual problem exists rather than focusing on something that is very, very unlikely to be the issue with your 6s execution time.  Have you taken these steps yet?  If so, are you able to show the portion of your benchmarking that has you believing the case structure selector is the issue?  (With that information, it's likely someone here is able to take a look at the benchmarking and find strengths/flaws in your decision making to help you get better benchmarking).

Message 13 of 20
(1,857 Views)

@Darin.K wrote:

wiebe@CARYA wrote:

@EngrStudent wrote:

I have a case structure.  Whether it is case-switch or if-then, it is a conditional selector of which subset of code to run.


It's not a case-switch nor an if-then. It's a case structure Smiley Very Happy.


Actually it is either a switch or an if-then, and it does make a difference. 


Why? EDIT: I mean why must it be a switch or an if-then?

 

A (C\C++) switch can match more then one option at once (you have to break each switch to prevent this), so it's not a switch.

 

It's not an if-then, because it always executes one case. So it's an if-then-else...

 

Not sure why we have to name it switch or if-then, it has a name, it's case structure.

 

But if you insist on calling it a if-then-else, please do so...

0 Kudos
Message 14 of 20
(1,822 Views)

@Jacobson-ni wrote:

I didn't know about that potential nesting optimization but I would be interested if that scaled beyond 2 nested structures.


So I had a nice reply filled complete with graphs, but when I go to post the forums chokes, tells me I am not logged in and loses everything....

 

No time to rehash it all, but yes it does carry on beyond 2, at least through 5.  There may well be an inflection point, but I'd go to C/C++ to meet timing requirements if I had to do more than 3-4 levels of nesting anyway.

 

Here is some more data as I vary the distribution of "True" and "False" strings.  0 = all "True" and 1 = all "False"

 

NestedCaseDistribution.png

 

The real culprit for the flat case structure slowdown seems to be a cache miss when a case is selected that is different than the previous iteration.  An array with a 50/50 split and random distribution is the worst-case scenario.  The same array, after sorting, is much faster.  The cost to sort is still there, obviously, this just helps to identify the culprit as a change in selection since the theoretical number of comparisons is the same in the two graphs.

 

Personally, I am inclined to nest structures in tight loops and be done with it the first time.  Given the IDE limitations, it is a real pain to go back if you really need this particular optimization.  It comes in handy many times when I am doing crypto operations (arbitrary precision math) as well as other 'sifting' or matching operations.

 

As to the idea of 'renaming', I have done no such thing.  'If-then-else' and 'switch' are mnemonics to remember the difference in behavior.  

 

As far as the OP is concerned, a so-called Engineering Student should probably be optimizing his own code to learn things the fun way.  That said, I would suggest starting by running the code with the performance monitor running to verify that the code is CPU-bound (running 1 or more cores at 100%) and/or bleeding memory (ever increasing RAM usage).  After a while you can get a feel for how long something should take, and 6 seconds is an eternity even on my laptop.  

Message 15 of 20
(1,779 Views)

Interesting, but I still feel like we're bike shedding.

 

Post the code that takes 6 seconds.

 

My bet is there are better opportunities to make it faster. Then again, if it is optimal, a few ms won't make a difference. 

0 Kudos
Message 16 of 20
(1,760 Views)

weibe@CARYA,

 

By "bike-shedding" do you mean this?  (It is a new term to me, so I had to ask)

https://en.wiktionary.org/wiki/bikeshedding

 

-EngrStudent

0 Kudos
Message 17 of 20
(1,739 Views)

@EngrStudent wrote:

wiebe@CARYA,

By "bike-shedding" do you mean this?  (It is a new term to me, so I had to ask)

https://en.wiktionary.org/wiki/bikeshedding


That's the one. Not to be confused with yak shaving.

 

We all love a challenge (, especially one without pressure). If you post the significant part of the code, we could have a look.

0 Kudos
Message 18 of 20
(1,726 Views)

weibe@CARYA,

 


We all love a challenge (, especially one without pressure). If you post the significant part of the code, we could have a look.


If I post the code, the folks who pay my bills will cut off some of my very important bodily appendages.  It would not be a good thing.

Sorry.

0 Kudos
Message 19 of 20
(1,714 Views)

@EngrStudent wrote:

weibe@CARYA,

 


We all love a challenge (, especially one without pressure). If you post the significant part of the code, we could have a look.


If I post the code, the folks who pay my bills will cut off some of my very important bodily appendages.  It would not be a good thing.

Sorry.


What will they do to you if a measurement takes months? Just saying, it might be worth it Smiley Very Happy.

 

I don't mind; less yaks to shave.

0 Kudos
Message 20 of 20
(1,704 Views)