<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Dirty hands coding - Comments: Performance comparison: linear search vs binary search</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/" rel="alternate"></link><link href="/feeds/comment.performance-comparison-linear-search-vs-binary-search.atom.xml" rel="self"></link><id>https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/</id><updated>2021-08-11T13:12:00+02:00</updated><entry><title>Posted by: Touisteur</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-13md" rel="alternate"></link><published>2021-08-11T13:12:00+02:00</published><updated>2021-08-11T13:12:00+02:00</updated><author><name>Touisteur</name></author><id>tag:dirtyhandscoding.github.io,2021-08-11:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-13md</id><content type="html">&lt;p&gt;Thanks for the link!&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: dirtyhandscoding</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-12md" rel="alternate"></link><published>2021-08-11T11:26:00+07:00</published><updated>2021-08-11T11:26:00+07:00</updated><author><name>dirtyhandscoding</name></author><id>tag:dirtyhandscoding.github.io,2021-08-11:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-12md</id><content type="html">&lt;p&gt;Yes, indeed it can be used to compute order statistics.&lt;/p&gt;
&lt;p&gt;For instance, see it &lt;a href="https://stackoverflow.com/questions/33307957/calling-stdnth-element-function-extremely-frequently/33325864#33325864"&gt;applied to median of 23 elements&lt;/a&gt;.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: Touisteur</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-11md" rel="alternate"></link><published>2021-08-11T00:45:00+02:00</published><updated>2021-08-11T00:45:00+02:00</updated><author><name>Touisteur</name></author><id>tag:dirtyhandscoding.github.io,2021-08-11:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-11md</id><summary type="html">&lt;p&gt;Hi, I'm wondering whether this technique could be used for selection tasks. Like 'find the N-th-biggest (or -smallest) element of a small (up to 64-elements) unsorted array' without sorting/moving the original data.&lt;/p&gt;
&lt;p&gt;For each &lt;code&gt;a[i]&lt;/code&gt; of the array &lt;code&gt;a[64]&lt;/code&gt; count (from 0 to 63) the number of …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Hi, I'm wondering whether this technique could be used for selection tasks. Like 'find the N-th-biggest (or -smallest) element of a small (up to 64-elements) unsorted array' without sorting/moving the original data.&lt;/p&gt;
&lt;p&gt;For each &lt;code&gt;a[i]&lt;/code&gt; of the array &lt;code&gt;a[64]&lt;/code&gt; count (from 0 to 63) the number of elements bigger (or smaller) than &lt;code&gt;a[i]&lt;/code&gt;. Then locate the one with value N with some other magic...&lt;/p&gt;
&lt;p&gt;Very, very insightful and inspiring.. Thanks!&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: RyanB</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-10md" rel="alternate"></link><published>2019-05-15T10:06:00+07:00</published><updated>2019-05-15T10:06:00+07:00</updated><author><name>RyanB</name></author><id>tag:dirtyhandscoding.github.io,2019-05-15:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-10md</id><summary type="html">&lt;p&gt;I came up with a little trick that seems to be 33% faster on my machine, for my use case (and it doesn’t require sorting):
In linear_search_scalar(), replace &lt;code&gt;arr[i] &amp;lt; key&lt;/code&gt; with &lt;code&gt;(arr[i] == key) * i&lt;/code&gt;.
Presumably, this allows the loop to just do one non-trivial addition operation (or …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I came up with a little trick that seems to be 33% faster on my machine, for my use case (and it doesn’t require sorting):
In linear_search_scalar(), replace &lt;code&gt;arr[i] &amp;lt; key&lt;/code&gt; with &lt;code&gt;(arr[i] == key) * i&lt;/code&gt;.
Presumably, this allows the loop to just do one non-trivial addition operation (or none if the item isn't to be found) instead of multiple.
Give it a try!&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: Dr. Khalid Omar Thabit</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-9md" rel="alternate"></link><published>2018-11-14T06:23:00+07:00</published><updated>2018-11-14T06:23:00+07:00</updated><author><name>Dr. Khalid Omar Thabit</name></author><id>tag:dirtyhandscoding.github.io,2018-11-14:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-9md</id><content type="html">&lt;p&gt;If prefetch instruction was used with the AVX2 in linear search it would even improve the speed.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: Krishty</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-8md" rel="alternate"></link><published>2018-04-01T02:35:00+07:00</published><updated>2018-04-01T02:35:00+07:00</updated><author><name>Krishty</name></author><id>tag:dirtyhandscoding.github.io,2018-04-01:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-8md</id><content type="html">&lt;p&gt;Thanks! I'm glad to hear they listened to your suggestion. Keep up the good work!&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: dirtyhandscoding</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-7md" rel="alternate"></link><published>2018-03-30T23:00:00+07:00</published><updated>2018-03-30T23:00:00+07:00</updated><author><name>dirtyhandscoding</name></author><id>tag:dirtyhandscoding.github.io,2018-03-30:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-7md</id><content type="html">&lt;p&gt;Yes, I suggested this change, and it got included into MSVC 15.6 =)&lt;/p&gt;
&lt;p&gt;I wrote about it in &lt;a href="/../../posts/addendum-to-performance-comparison-linear-search-vs-binary-search.html"&gt;the addendum&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Rerunning tests is not necessary: the revised results include a pretty good workaround.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: Krishty</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-6md" rel="alternate"></link><published>2018-03-30T17:01:00+07:00</published><updated>2018-03-30T17:01:00+07:00</updated><author><name>Krishty</name></author><id>tag:dirtyhandscoding.github.io,2018-03-30:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-6md</id><content type="html">&lt;p&gt;FYI: Since the recent Visual Studio 15.6 update, Visual C++ has started to emit &lt;code&gt;MOV -1&lt;/code&gt; instead of &lt;code&gt;OR -1&lt;/code&gt; (if compiled for speed).&lt;/p&gt;
&lt;p&gt;Your article must have had some impact. Maybe you want to re-run your tests?&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: dirtyhandscoding</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-5md" rel="alternate"></link><published>2017-09-12T23:04:00+07:00</published><updated>2017-09-12T23:04:00+07:00</updated><author><name>dirtyhandscoding</name></author><id>tag:dirtyhandscoding.github.io,2017-09-12:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-5md</id><summary type="html">&lt;p&gt;What I mean is: "the number returned by this code would be the same no matter how you shuffle the input array". Indeed, it does not necessarily mean that this number would be useful for a non-sorted array.&lt;/p&gt;
&lt;p&gt;In your case the answer would be 2 regardless of the order …&lt;/p&gt;</summary><content type="html">&lt;p&gt;What I mean is: "the number returned by this code would be the same no matter how you shuffle the input array". Indeed, it does not necessarily mean that this number would be useful for a non-sorted array.&lt;/p&gt;
&lt;p&gt;In your case the answer would be 2 regardless of the order of elements.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: A Reader</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-4md" rel="alternate"></link><published>2017-09-12T21:34:00+07:00</published><updated>2017-09-12T21:34:00+07:00</updated><author><name>A Reader</name></author><id>tag:dirtyhandscoding.github.io,2017-09-12:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-4md</id><summary type="html">&lt;p&gt;I have a question about "In fact, this criterion would work the same way even if you shuffle the input array randomly"&lt;/p&gt;
&lt;p&gt;If my array is &lt;code&gt;[10, 2, 1]&lt;/code&gt; and my key is 10, then the number of elements smaller than the key is 2, but this is not the …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I have a question about "In fact, this criterion would work the same way even if you shuffle the input array randomly"&lt;/p&gt;
&lt;p&gt;If my array is &lt;code&gt;[10, 2, 1]&lt;/code&gt; and my key is 10, then the number of elements smaller than the key is 2, but this is not the index of the key. Am I missing something? The way I see it, is that this counting search requires a sorted array.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: dirtyhandscoding</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-3md" rel="alternate"></link><published>2017-08-26T19:06:00+07:00</published><updated>2017-08-26T19:06:00+07:00</updated><author><name>dirtyhandscoding</name></author><id>tag:dirtyhandscoding.github.io,2017-08-26:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-3md</id><summary type="html">&lt;p&gt;Yes, this is the official way of using IACA under MSVC x64. However, when you use &lt;code&gt;IACA_VC64_START&lt;/code&gt;, you cannot guarantee that the marker would get exactly at the beginning of the loop in assembly (same issue for end marker), compiler can and does move it around freely. Maybe 32-bit case …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Yes, this is the official way of using IACA under MSVC x64. However, when you use &lt;code&gt;IACA_VC64_START&lt;/code&gt;, you cannot guarantee that the marker would get exactly at the beginning of the loop in assembly (same issue for end marker), compiler can and does move it around freely. Maybe 32-bit case also has this problem...&lt;/p&gt;
&lt;p&gt;When you have a small loop and compiler moves marker away, you get quite different analysis. Moreover, sometimes inter-iteration dependencies are completely incorrect because a few first instructions of the loop are missing from the analysed section of assembly.&lt;/p&gt;
&lt;p&gt;I used these macros myself in the past. Now I know reliable way of setting the markers exactly where I want, so I’m done with the macros.&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: optimizedaway</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-2md" rel="alternate"></link><published>2017-08-26T18:16:00+07:00</published><updated>2017-08-26T18:16:00+07:00</updated><author><name>optimizedaway</name></author><id>tag:dirtyhandscoding.github.io,2017-08-26:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-2md</id><content type="html">&lt;p&gt;IACA runs just fine for 64 bit exes, use &lt;code&gt;IACA_VC64_START&lt;/code&gt;, &lt;code&gt;IACA_V64_END&lt;/code&gt; defines and &lt;code&gt;-64&lt;/code&gt; command line arg i.e. &lt;code&gt;iaca -64 -arch HSW MyProgram.exe&lt;/code&gt;&lt;/p&gt;</content><category term="Uncategorized"></category></entry><entry><title>Posted by: demofox2</title><link href="https://dirtyhandscoding.github.io/posts/performance-comparison-linear-search-vs-binary-search.html/#comment-1md" rel="alternate"></link><published>2017-08-25T23:44:00+07:00</published><updated>2017-08-25T23:44:00+07:00</updated><author><name>demofox2</name></author><id>tag:dirtyhandscoding.github.io,2017-08-25:/posts/performance-comparison-linear-search-vs-binary-search.html//comment-1md</id><content type="html">&lt;p&gt;Nice analysis. Interesting stuff!&lt;/p&gt;</content><category term="Uncategorized"></category></entry></feed>