Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question
Which has greater time complexity, a for loop or a while loop?
Can anyone give me a chart to compare the time complexity of different loops?
If possible suggest good references to learn about time complexity.
Barring a few technicalities, a while loop and a for loop are isomorphic:
for (a; b; c) {
body;
}
can be turned into:
a;
while (b) {
body;
c;
}
As such, there's no difference in computational complexity between the two.
As to choosing between the two: the usual rule of thumb is to use a for loop if at least two of the three clauses are meaningful for that loop.
If you are doing the exact same thing with them, they should be pretty much exactly the same. For instance:
int i = 0;
while (i < 10)
i++;
and
for (int i = 0; i < 10; i++);
should have pretty much exactly the same time complexity, since they are doing the exact same thing.
The only thing that changes the time complexity would be what is inside of the loop. But there's really no reason to favor for over while other than it adds a convenient way to declare and iterate a variable.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
It is well known that switch-case constructs can provide better code performance than if-else constructs due to implementation in the form of jump-tables,etc. You can know this only when you know how the compiler implements switch-case. So my question is that how do you get to know how a compiler, for example, Microsoft C++ Compiler or g++, implements a feature? Is there some standard literature available on these topics with respect to common compilers?
It is not very useful to get information on how will a compiler implement a feature because the compiler runs multiple steps, each will modify the compiled result.
As an example:
A first step build up a meta language, a second step do a first optimization, next step maybe inline some code, next step...
So you can't get any idea of the code which will be created. So only chance you have: Try it out!
For your example of a switch/case it is important if
the case patterns are linear
the code inside the pattern is used once or multiple
the code return or modify variables or only call other functions
4 ... tons of other dependencies!
Forget about prediction of optimizer results.
For gcc you have the source, look inside :-)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
Are there any tricks that can work with large pile of metaprogramming code?
I'm using the latest version of clang. But I can switch to any (free) compiler that support C++14.
Or, are there any tricks that can work with large pile of
metaprogramming code?
Prefer constexpr-functions to TMP-functions, they are generally faster. Since C++14 you can also use more than only a return-statement of working code in constexpr-functions, that eases their use even more.
Generally avoid recursion. And don't care about the complexity class of the algorithm itself too much, as long as it compiles fast. (In TMP code, getting the better complexity class can result in much slower compilation).
Don't use recursion with variadic templates like so:
template< typename First, typename ... Tail >
struct A : A<Tail...>
They create a quadratic time complexity, because the compiler has to create separate argument lists in every step! This is significantly measurable. Try instead to delegate recursion to one point in the program, the best approach is to delegate it to std::make_index_sequence, and then deduce everything to that.
Don't use Boost.PP to generate many partial/explicit specializations which handle cases. It doesn't help. 200 explicit spezializations will only make it worse, since the compiler has to search through the spezializations (beside the primary template) and look for the first and most specialized one which matches.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's say I have a variable a and b. Now I want to find a value for c such that the fraction a / c is a positive integer, and where c is as close to b as possible.
For example: a = 100 and b = 30.
In this case I want c to be 25; because a / c is an integer, and c is as close as b for which this holds.
Any ideas how I can program a function in C++ which does exactly this?
Find the factors of a. (search web for methods)
Scan resulting list for minimum difference vs b.
Is this a homework assignment? Either way, think about how you would solve this problem without writing any code. A good algorithm comes from a good design. Break the problem down into pieces and walk through some more examples. For example, how would you solve the problem of determining whether the division results in an integer value? Hint: There is a different operator you could use as opposed to division to achieve this easily. Now, how would you solve the problem of determining what number to start at for c in the algorithm? Do not write any code until you have the pseudocode figured out.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for book titles or papers about how to decompile X86 Disassembly into C/C++ code MANUALLY.
Well , I know about many tools that do the job , but I think doing it manually is more efficient even if it's a slow process.
If you are VERY used to looking at disassembly from a particular compiler, you MAY be able to come up with decent C or C++ code. But it's a TERRIBLY slow process even then. Just taking one small function (say a for-loop, a couple of if-statements and some basic math) and reconstructing it back into source code can take half an hour for me, and I don't think I'm terribly bad at it. Of course, one of the main points is identifying commonly used algorithms, such as linked lists, binary trees, string management, vector management, etc.
Doing it by machine will give you a lot of the work done for you, but even then, it can take days to do even a few hundred lines of orginal C++ code into something that is actually readable.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In C/C++, I was wondering which is faster?
int a;
int b = a + a; // this
int b = 2 * a; // or this?
Also, is important the datatype? What about long? what about the number of times we add up?
(what about...)
long a;
long b = a + a + a +a;
long b = 4 *a;
Trust your optimizing compiler. It knows how to optimize for a specific CPU/architecture in ways that you will only be able to guess. Without reference to a specific architecture, there is no meaning to statements like "is x faster than y?", because it all depends on a huge number of factors.
And as always with performance questions, measuring is going to answer the question more completely than us offering semi-informed opinions and guesses.