Which is more optimized code? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
// (1)
for (int iter = 1; iter <= VERTEX_SIZE; iter ++) {
if (visit[iter]) continue;
dfs(iter);
}
// (2)
for (int iter = 1; iter <= VERTEX_SIZE; iter ++) {
if (!visit[iter]) {
dfs(iter);
}
}
Which code is more optimized? I'm just curious about it.

With any even semi-decent compiler this won’t make a difference at all; the generated machine code will be exactly the same. If you still want to be sure, benchmark it.
Go for the one you find most readable.

There is no difference. Pick the one that you thing is the best. Every program you compile with the first way will definitely work with the second way too, and the running time will be the same.
Note: I would use the second way; I thing it's more readable and the majority of implementations of DFS I have ever seen (in C++ and in pheudocode) uses it.

Related

what is the complexity of the following code? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the complexity of the following piece of code??
for (int i = 1; i * i <= n; i++)
{
if (n%i == 0)
//do anything
}
The loop runs √n times, and the conditional is met every time i is a factor of n — the latter is a non-trivial condition and needs to be analysed carefully. It depends on the prime factorisation of n. For example, if n is prime, the condition is only true once, for i == 1, and never again.

Is the "!=" comparision operator faster than ">"? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
A really basic question:
Considering an unsigned integer value, we would like to check that is not equal to 0. Using != or >, which one would be more efficient to use in C++?
Is your application too slow? If it is, the first thing you should do is profile -- this will show you what is causing your program to be slow.
If you aren't having efficiency issues with your program then you shouldn't be worried about this. In fact, worrying about speed at this stage is a bad thing because often people write less readable code in an attempt to improve speed when it's not even an issue.

Palindrome without using extra space [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I know various ways to check if an integer is a palindrome or not by using string conversion, stack and number breaking, but here question is
"How can we check wether an integer is a palindrome or not, without using any extra space?"
You can revert a number with some code like:
int revert(int num) {
int reverted = 0;
while (num) {
reverted = reverted*10 + num%10;
num /= 10;
}
return reverted;
}
And now you only check if
num == revert(num)
That is all. Sorry for giving the exact solution instead of just a tip, but I don't think I could have given any tip without the solution itself.

How do I make a smaller range with two given integers? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?

print a series of numbers optimization part 2 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
earlyer i posted part 1 and got some interesting responces
print a series of numbers optimization part 1
here is another way you could have the program print a repeating series of numbers to the screen, the goal here is to make the most efficiant/fastest algorithm
int series[] = [2,3,4,5,6,7,8,9,1]
int i = 9;
while(true)
{
print(series[i])
i = series[i] - 1;
}
of course ignore any extra overhead created by actually printing the number because that is not the purpose of the problem
the one boolean conditional statement (while true) is required for the infinite loop is required no matter what solution you do, so you can ignore that too
this solution uses memory for 11 int variables, but otherwise it only does one simple computation and one variable assignment per iteration.
so would this be the most time efficiant way to solve the infiniate number series problem?
I would say it's not the most efficient way.
There's a multiplication involved in addressing the array. It's essentially
destinationAddress = baseAddressOfArray + indexRequested * sizeof(elementOfArray)
I think the most efficient way would be to cache the string of one iteration and simply spit out that string over and over again. I'm not up on my exact C++ syntax, it'd be something like
string s = "123456789";
while(true) {
print(s);
}