<p>In Visual Basic, is there a performance difference when using the <code>IIf</code> function instead of the <code>If</code> statement?</p>

[{'answer_id': 28383, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 3, 'selected': False, 'text': '<p>According to <a href="http://www.vb-helper.com/howto_compare_iif_ifthen_speeds.html" rel="noreferrer">this guy</a>, IIf can take up to 6x as long as If/Then. YMMV.</p>\n'}, {'answer_id': 28393, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 3, 'selected': False, 'text': "<p>...as to why it can take as long as 6x, quoth the wiki:</p>\n\n<blockquote>\n  <p>Because IIf is a library function, it\n  will always require the overhead of a\n  function call, whereas a conditional\n  operator will more likely produce\n  inline code.</p>\n</blockquote>\n\n<p>Essentially IIf is the equivalent of a ternary operator in C++/C#, so it gives you some nice 1 line if/else type statements if you'd like it to. You can also give it a function to evaluate if you desire.</p>\n"}, {'answer_id': 28405, 'author': 'EdgarVerona', 'author_id': 3068, 'author_profile': 'https://Stackoverflow.com/users/3068', 'pm_score': 3, 'selected': False, 'text': '<p>On top of that, readability should probably be more highly preferred than performance in this case.  Even if IIF was more efficient, it\'s just plain less readable to the target audience (I assume if you\'re working in Visual Basic, you want other programmers to be able to read your code easily, which is VB\'s biggest boon... and which is lost with concepts like IIF in my opinion).</p>\n\n<p>Also, <a href="http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx" rel="noreferrer">"IIF is a function, versus IF being part of the languages\' syntax"</a>... which implies to me that, indeed, If would be faster... if for nothing else than that the If statement can be boiled down directly to a small set of opcodes rather than having to go to another space in memory to perform the logic found in said function.  It\'s a trite difference, perhaps, but worth noting.</p>\n'}, {'answer_id': 28421, 'author': 'Tom Mayfield', 'author_id': 2314, 'author_profile': 'https://Stackoverflow.com/users/2314', 'pm_score': 6, 'selected': False, 'text': '<p><code>IIf()</code> runs both the true and false code. For simple things like numeric assignment, this isn\'t a big deal. But for code that requires any sort of processing, you\'re wasting cycles running the condition that doesn\'t match, and possibly causing side effects.</p>\n\n<p>Code illustration:</p>\n\n<pre><code>Module Module1\n    Sub Main()\n        Dim test As Boolean = False\n        Dim result As String = IIf(test, Foo(), Bar())\n    End Sub\n\n    Public Function Foo() As String\n        Console.WriteLine("Foo!")\n        Return "Foo"\n    End Function\n\n    Public Function Bar() As String\n        Console.WriteLine("Bar!")\n        Return "Bar"\n    End Function\nEnd Module\n</code></pre>\n\n<p>Outputs:</p>\n\n<pre><code>Foo!\nBar!\n</code></pre>\n'}, {'answer_id': 28435, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 4, 'selected': False, 'text': '<p>Also, another big issue with the IIf is that it will actually call any functions that are in the arguments [1], so if you have a situation like the following:</p>\n\n<pre><code>string results = IIf(Not oraData.IsDBNull(ndx), oraData.GetString(ndx), string.Empty)\n</code></pre>\n\n<p>It will actually throw an exception, which is not how most people think the function works the first time that they see it. This can also lead to some very hard to fix bugs in an application as well.</p>\n\n<p>[1] IIf Function - <a href="http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/27ydhh0d(VS.71).aspx</a></p>\n'}, {'answer_id': 28452, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 8, 'selected': True, 'text': '<p>VB has the following <code>If</code> statement which the question refers to, I think:</p>\n\n<pre><code>\' Usage 1\nDim result = If(a &gt; 5, "World", "Hello")\n\' Usage 2\nDim foo = If(result, "Alternative")\n</code></pre>\n\n<p>The first is basically C#\'s ternary conditional operator and the second is its coalesce operator (return <code>result</code> unless it’s <code>Nothing</code>, in which case return <code>"Alternative"</code>). <code>If</code> has thus replaced <code>IIf</code> and the latter is obsolete.</p>\n\n<p>Like in C#, VB\'s conditional <code>If</code> operator short-circuits, so you can now safely write the following, which is not possible using the <code>IIf</code> function:</p>\n\n<pre><code>Dim len = If(text Is Nothing, 0, text.Length)\n</code></pre>\n'}, {'answer_id': 1459868, 'author': 'Larry', 'author_id': 24472, 'author_profile': 'https://Stackoverflow.com/users/24472', 'pm_score': 3, 'selected': False, 'text': '<p>Better use If instead of IIf to use the type inference mechanism correctly (Option Infer On)</p>\n\n<p>In this example, Keywords is recognized as a string when I use If :</p>\n\n<pre><code>Dim Keywords = If(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)\n</code></pre>\n\n<p>Otherwise, it is recognized as an Object :</p>\n\n<pre><code>Dim Keywords = IIf(String.IsNullOrEmpty(SelectedKeywords), "N/A", SelectedKeywords)\n</code></pre>\n'}, {'answer_id': 1940410, 'author': 'some guy', 'author_id': 236074, 'author_profile': 'https://Stackoverflow.com/users/236074', 'pm_score': 3, 'selected': False, 'text': '<p>I believe that the main difference between If and IIf is:</p>\n\n<ul>\n<li><p>If(test [boolean], statement1, statement2) \nit means that according to the test value either satement1 or statement2 will executed\n(just one statement will execute)</p></li>\n<li><p>Dim obj = IIF(test [boolean] , statement1, statement2)\nit means that the both statements will execute but according to test value one of them will return a value to (obj).</p></li>\n</ul>\n\n<p>so if one of the statements will throw an exception it will throw it in (IIf) anyway but in (If) it will throw it just in case the condition will return its value. </p>\n'}, {'answer_id': 29085777, 'author': 'titol', 'author_id': 4193860, 'author_profile': 'https://Stackoverflow.com/users/4193860', 'pm_score': 1, 'selected': False, 'text': '<p>Those functions are different! Perhaps you only need to use IF statement.\nIIF will always be slower, because it will do both functions plus it will do standard IF statement.</p>\n\n<p>If you are wondering why there is IIF function, maybe this will be explanation:</p>\n\n<pre><code>Sub main()\n    counter = 0\n    bln = True\n    s = iif(bln, f1, f2)\nEnd Sub\n\nFunction f1 As String\n    counter = counter + 1\n    Return "YES"\nEnd Function\n\nFunction f2 As String\n    counter = counter + 1\n    Return "NO"\nEnd Function\n</code></pre>\n\n<p>So the counter will be 2 after this, but s will be "YES" only. I know this counter stuff is useless, but sometimes there are functions that you will need both to run, doesn\'t matter if IF is true or false, and just assign value from one of them to your variable.</p>\n'}]
['https://Stackoverflow.com/questions/28377', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/299/']