Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Accelerated Search For the Nearest Line


Discussion

In the sample we reduced the total runtime from 8 days to 21 seconds (see Table 2 for details). The sample data of course is different from real-world data (i.e., from a GIS). But the runtime improvements shown here are comparable to those we encountered with data from GIS. Batch processing usually not only searches in data, but it also performs some action on it, which of course costs runtime that is not reduced by improving search. In the task I had to perform, the overall task was reduced from a number of days to a few hours. Also impressive is the difference between optimized and debug compiled code, which came to about factor 10.

Table 2: Comparison of search and batch processing times.

In this solution it becomes crucial to set the right width of the single quadrants: Setting them too small (i.e., 10 meter in the sample) increases the memory and the time to setup the data of CQuadMap. Setting it too large (i.e., 100 meter) more and more reduces the acceleration introduced by the algorithm. On real-world data I found that 25 meters is more appropriate. Below 10 meters, the amount of memory used soon reaches 1 GB and more. Keep in mind, that enlarging the size of the quadrants does not affect the search result though: It only leads to more lines checked as candidate, but it will still return the nearest element. Also still only those elements are returned that are not more far away than the maximum search width, even if the quadrant size is larger then this. It is illegal though to use a smaller quadrant size than the maximum search width.

Of course your units used can also be micrometers or miles.

The time to execute linear search grows linear with the number of lines used. When connecting all lines to each other it grows exponentially by n^2. The search time of CQuadMap turns out to be more or less linear and independent of the number of lines inserted. Connecting 1,000,000 lines to each other of course is a heavy task, but also when doing this with only 10,000 lines the runtime is reduced impressively from more than one minute to 188ms -- making such a task possible in real time (see table 2).

Conclusion

In this article I introduced a algorithm that reduces the search time for the next line of a given point to a fraction compared to linear search. The performance gain lays between 420 and 36,000 depending on the number of lines existing. The algorithm is implemented in C++ and publicly available.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.