Note that there are some explanatory texts on larger screens.

plurals
  1. POTry/catch or validation for speed?
    primarykey
    data
    text
    <p>I'm working with Python and whenever I've had to validate function input, I assumed that the input worked, and then caught errors.</p> <p>In my case, I had a universal <code>Vector()</code> class which I used for a few different things, one of which is addition. It functioned both as a <code>Color()</code> class and as a <code>Vector()</code>, so when I add a scalar to the <code>Color()</code>, it should add that constant to each individual component. <code>Vector()</code> and <code>Vector()</code> addition required component-wise addition.</p> <p>This code is being used for a raytracer so any speed boosts are great.</p> <p>Here's a simplified version of my <code>Vector()</code> class:</p> <pre><code>class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, other): try: return Vector(self.x + other.x, self.y + other.y, self.z + other.z) except AttributeError: return Vector(self.x + other, self.y + other, self.z + other) </code></pre> <p>I'm currently using the <code>try...except</code> method. Does anybody know of a faster method?</p> <hr> <p><strong>EDIT:</strong> Thanks to the answers, I tried and tested the following solution, which checks specifically for a class name before adding the <code>Vector()</code> objects:</p> <pre><code>class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, other): if type(self) == type(other): return Vector(self.x + other.x, self.y + other.y, self.z + other.z) else: return Vector(self.x + other, self.y + other, self.z + other) </code></pre> <hr> <p>I ran a speed test with these two blocks of code using <a href="http://docs.python.org/library/timeit.html" rel="noreferrer"><code>timeit</code></a>, and the results were pretty significant:</p> <pre><code> 1.0528049469 usec/pass for Try...Except 0.732456922531 usec/pass for If...Else Ratio (first / second): 1.43736090753 </code></pre> <p>I haven't tested the <code>Vector()</code> class with <em>no</em> input validation whatsoever (i.e. moving the checking out of the class and into the actual code), but I'd imagine that it's even faster than the <code>if...else</code> method.</p> <hr> <p><strong>Late update</strong>: Looking back at this code, this is <em>not</em> an optimal solution.</p> <p>OOP makes this even faster:</p> <pre><code>class Vector: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def __add__(self, other): return Vector(self.x + other.x, self.y + other.y, self.z + other.z) class Color(Vector): def __add__(self, other): if type(self) == type(other): return Color(self.x + other.x, self.y + other.y, self.z + other.z) else: return Color(self.x + other, self.y + other, self.z + other) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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