You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
|
-
2Right, but the setup cost of sorting the array is O(N log N), so breaking early doesn't help you if the only reason you are sorting the array is to be able to break early. If, however, you have other reasons to pre-sort the array, then yes, this is valuable.– Luke HutchisonNov 6 '18 at 12:28
-
1Depends how many times you sort the data compared to how many times you loop on it. The sort in this example is just an example, it doesn't have to be just before the loop– Yochai TimmerFeb 27 '19 at 12:23
-
3Yes, that's exactly the point I made in my first comment :-) You say "The branch prediction will miss only once." But you are not counting the O(N log N) branch prediction misses inside the sort algorithm, which is actually greater than the O(N) branch prediction misses in the unsorted case. So you would need to use the entirety of the sorted data O(log N) times to break even (probably actually closer to O(10 log N), depending on the sort algorithm, e.g. for quicksort, due to cache misses -- mergesort is more cache-coherent, so you would need closer to O(2 log N) usages to break even.)– Luke HutchisonFeb 28 '19 at 12:28
-
1One significant optimization though would be to do only "half a quicksort", sorting only items less than the target pivot value of 127 (assuming everything less than or equal to the pivot is sorted after the pivot). Once you reach the pivot, sum the elements before the pivot. This would run in O(N) startup time rather than O(N log N), although there will still be a lot of branch prediction misses, probably of the order of O(5 N) based on the numbers I gave before, since it's half a quicksort.– Luke HutchisonFeb 28 '19 at 12:34
- ► fix grammatical or spelling errors
- ► clarify meaning without changing it
- ► correct minor mistakes
- ► add related resources or links
- ► always respect the original author
► create code fences with backticks ` or tildes ~
like so
```
► add language identifier to highlight code
def function(foo):
print(foo)
```
► put returns between paragraphs
► for linebreak add 2 spaces at end
► _italic_ or **bold**
► indent code by 4 spaces
► backtick escapes `like _so_`
► quote by placing > at start of line
► to make links (use https whenever possible)
<https://example.com>
[example](https://example.com)
<a href="https://example.com">example</a>
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
► complete the sentence: my question is about...
► use tags that describe things or concepts that are essential, not incidental to your question
► favor using existing popular tags
► read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
► combine multiple words into single-words with hyphens (e.g. ruby-on-rails), up to a maximum of 35 characters
► creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you