Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answers given thus far don't really fully answer the question. <code>try...catch</code> vs a validating <code>if then</code> can result in different performance (see: <a href="https://stackoverflow.com/a/5591737/456188">https://stackoverflow.com/a/5591737/456188</a>). To summarize that answer: depends on the ratio of failures to successes and the <em>MEASURED</em> time of a failure and success in both cases. Basically we can't answer this, but we can tell you how to:</p> <ol> <li>Look at a few representative cases to get a ratio.</li> <li>Write an <code>if/then</code> that tests the same as the <code>try/catch</code> optimize it and then measure how long it takes both version of the <code>to_float_or_None</code> to fail 100 times and measure how long it takes both versions of the <code>to_float_or_None</code> to succeed 100 times.</li> <li>Do a little math to figure out which will be faster.</li> </ol> <p>Side note about the list comprehension issue:</p> <p>Depending on whether the you want to be able to index the results of this, or whether you just want to iterate over it a generator expression would actually be even better than a list comprehension (just replace the <code>[</code> <code>]</code> characters with <code>(</code> <code>)</code> characters).</p> <p>It takes essentialy no time to create, and the actual execution of to_float_or_None (which is the expensive part) can be delayed until the result it needed.</p> <p>This is useful for many reasons, but won't work if you're going to need to index it. It <em>will</em> however, allow you to zip the original collection with the generator so you can still have access to the original string along with its float_or_none result.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload