Tag Info

Hot answers tagged

76

Every processor I've worked on does comparison by subtracting one of the operands from the other, discarding the result and leaving the processor's flags (zero, negative, etc.) alone. Because subtraction is done as a single operation, the contents of the operands don't matter. The best way to answer the question for sure is to compile your code into ...


22

Is there a difference in performance on the ALU level in comparisons between very large numbers vs very small ones? It's very unlikely, unless going from a small number to a large number changes your numeric type, say from an int to a long. Even then, the difference might not be significant. You're more likely to see a difference if your programming ...


15

Many processors have "small" instructions which can perform arithmetic operations, including comparisons, on certain immediately-specified operands. Operands other than those special values must either use a larger instruction format or, in some cases, must use a "load value from memory" instruction. In the ARM Cortex-M3 instruction set, for example, there ...


10

Let's have a look at the second claim. The JavaScript reactor pattern will always handle concurrency better than any multi-threaded application. To address this claim, I'm going to assume that concurrency in this context equates to scalability, since scalability is one of the primary motivations behind Node.JS. The distinction is subtle, but ...


5

The short answer to this question is, no, there's no time difference to compare two numbers based on the magnitude of those numbers assuming they're stored in the same data type (e.g. both 32-bit ints or both 64-bit longs.) Furthermore, up to the word size of the ALU, it's incredibly unlikely that comparing two integers to each other will ever take more ...


4

The main drawback of generators is they can only be traversed in one direction. There's no going back to a previous value. You also can't share them. There are many cases where that can easily be accounted for, or even where it is preferable, but there are also many cases where it isn't. Sorting, for example. That's why a lot of times you'll see ...


3

Yes, it can. The C language specification requires conforming compilers to assume that the programmer can do various aliasing things, that are in fact extremely brain-dead in 99% of cases, and generate correct code anyway. This makes things harder for the compiler writer and makes the resulting generated code slower. The FORTRAN language specification ...


3

@RobertHarvey's answer is good; consider this answer a supplement to his. You should also consider Branch Prediction: In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow ...


2

What "polynomial time" means is that if you calculate a formula describing how long the algorithm will take to complete on a data set with n elements, the largest-magnitude term in the formula will be in the form of n^x. Here are a few examples of runtime classes: O(1): the runtime does not get larger as the data set increases in size. Example: Array ...


2

Stroustrup gives a few hints about performance in his paper Myths of C++ (pdf) including one about performance of C being faster than C++, with code. I suppose you can say "anything you can do in C++ you can do in C therefore it can always be as fast", but that assumes optimisations that are either already done for you, or specialisms that would be ...


2

You've got iterators confused with generators. Your first example is a list iterator expression while the second is a generator expression. The key difference is that the generator creates each member of the given collection lazily (as needed) rather than eagerly (at once, whether needed or not). You can define your own generators by using yield rather than ...


2

It depends on the implementation, but it would be very, very unlikely. I admit that I have not read through the implementation details of the various browser engines, and CSS does not specify any particular type of storage for numbers. But I believe that it is safe to assume that all of the major browsers are using 64-bit double-precision floating-point ...


2

Well this answer on ServerFault shows a single rule with many IP blocks works fine, but as he says it take a lot of CPU to process when updating, I imagine the rules are stored internally in an efficient format, and when the rule is changed WF will re-parse and store the IPs. In this case, it doesn't matter if you have 1 rule with a million IPs or a million ...


1

Ultimately, it boils down to "how to optimize when there is not a single metric that is gold-standard?" For the general case, one must use the scientific method, just like the protein researchers do. Firstly, you should have two goals: In the competitive comparison of algorithms, you want to know whether Algorithm A is faster than algorithm B, in ...


1

Storing files in memory doesn't seem such a bad idea if there are only a few of them. 1Mb of data isn't very much on modern servers so it's all down to the level of simultaneous users you have and what happens when you 'run out' of memory. In my experience I try to reduce the memory footprint size of each web service request because the server is usually a ...


1

A classic example of an O(n³) algorithm is matrix multiplication. Multiplying two 10x10 matrices together will require 10^3 multiplications. In practice, however, modern algorithms such as the Coppersmith–Winograd algorithm can bring the order down to about 2.37.


1

Consider finding the smallest element in an unsorted collection. While you could use an array or linked list to hold the data, the time complexity will be O(n) as each element has to be examined as that could be the smallest that hasn't been known yet.


1

One of the most common ways to express polynomial time, O(nk), is nested loops where n is used as an input to calculate the maximum iterations of each loop and k is the level of nested loops. For example, this loop is O(n2) because it is a doubly-nested loop where n is the number of iterations: int n = ...; int x = 0; for (int i = 0; i < n; ++i) { for ...



Only top voted, non community-wiki answers of a minimum length are eligible