16
votes
3answers
902 views

Is “==” in sorted array not faster than unsorted array? [duplicate]

Note: the alleged duplicate question is, I think, mostly related to "<" and ">" comparison, but not "==" comparison and hence does not answer my question about performance of the "==" operator. ...
7
votes
1answer
404 views

Complexity of comparison operators [duplicate]

Why does the y[i] < x[i] function take twice the time when array x is always higher in value than y (for ex 1<x<2, and 0<y<1). In addition, when comparing 0.5<x<1.5, and 0<y<...
-1
votes
0answers
56 views

Search for element in very huge ArrayList in java: Out of memory error [duplicate]

I have a very big ArrayList in Java (let's call it ArrayA), it have three million elements. I have another ArrayList in Java, that is slightly bigger than arrayA, it have three and a half million ...
2105
votes
2answers
189k views

Why is printing “B” dramatically slower than printing “#”?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i ...
900
votes
8answers
93k views

Replacing a 32-bit loop count variable with 64-bit introduces crazy performance deviations

I was looking for the fastest way to popcount large arrays of data. I encountered a very weird effect: Changing the loop variable from unsigned to uint64_t made the performance drop by 50% on my PC. ...
692
votes
5answers
109k views

How to determine whether my calculation of pi is accurate?

I was trying various methods to implement a program that gives the digits of pi sequentially. I tried the Taylor series method, but it proved to converge extremely slowly (when I compared my result ...
537
votes
9answers
83k views

What is “cache-friendly” code?

Could someone possibly give an example of "cache unfriendly code" and the "cache friendly" version of that code? How can I make sure I write cache-efficient code?
218
votes
22answers
13k views

Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?

Are there are any cases where you would prefer O(log n) time complexity to O(1) time complexity? Or O(n) to O(log n)? Do you have any examples?
230
votes
15answers
256k views

A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]

See Related .NET question I'm looking for a quick and easy way to do exactly the opposite of split so that it will cause ["a","b","c"] to become "a,b,c" Iterating through an array requires either ...
310
votes
6answers
55k views

Why does gcc generate 15-20% faster code if I optimize for size instead of speed?

I first noticed in 2009 that gcc (at least on my projects and on my machines) have the tendency to generate noticeably faster code if I optimize for size (-Os) instead of speed (-O2 or -O3), and I ...
221
votes
9answers
32k views

Ternary operator is twice as slow as an if-else block?

I read everywhere that ternary operator is supposed to be faster than, or at least the same as, its equivalent if-else block. However, I did the following test and found out it's not the case: ...
212
votes
10answers
82k views

likely()/unlikely() macros in the Linux kernel - how do they work? What's their benefit?

I've been digging through some parts of the Linux kernel, and found calls like this: if (unlikely(fd < 0)) { /* Do something */ } or if (likely(!err)) { /* Do something */ } I've found ...
156
votes
9answers
20k views

Good or bad practice? Initializing objects in getter

I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example): [Serializable()] public ...
109
votes
11answers
20k views

Do sealed classes really offer performance Benefits?

I have come across a lot of optimization tips which say that you should mark your classes as sealed to get extra performance benefits. I ran some tests to check the performance differential and found ...
76
votes
7answers
11k views

If statement vs if-else statement, which is faster? [closed]

I argued with a friend the other day about those two snippets. Which is faster and why ? value = 5; if (condition) { value = 6; } and: if (condition) { value = 6; } else { value = 5; } ...

15 30 50 per page