Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i was just playing with the factorial question using recursion when i wrote the following code.
i know that i could directly return the factorial.however i created a variable result and wrote the code below.
now what i want to know is that haven't i created n (the no. i want to calculate the factorial of)no. of result variables in the process?? because whenever my function factorial is called ,result variable is created , and each of those result variables would hold some value.
long long factorial(long long param) {
long long result;
if (param == 1) return 1;
else {
result = param * factorial(param - 1);
}
return result;
}
i know this is not a good code and i didn't thought that this would give me the write answer .however to my surprise it is.i want to know what is going on in this program.
Your function is a recursive function. You can read about recursion, and about recursive debugging here:
https://www.programiz.com/cpp-programming/recursion
https://beginnersbook.com/2017/08/cpp-recursion/
First of all : your function is unable to determine 0!
Second, yes, without any optimization from the compiler your program will take up unnecessary resources. The function is called n times and so the stack grows n times. Within each stack frame a temporary result is pushed on the stack.
However, with this program being so small, it is very likely that a minimal compiler effort will optimize that away in a release build.
It is also possible to do a recursion without the stack growing : define your factorial in such a way that there are never temporary values involved. If f(n, a) := n == 0 ? a : f(n-1, n*a) then factorial(n) := f(n, 1)
This recursion just keeps an accumulated result, wich is a fine example of functional programming. The stack needn't grow.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
There have already been some questions on this topic (1, 2, 3). The thing is that there doesn't seem to be a clear-cut answer. Some answers suggest size_t (1, 2), some suggest ptrdiff_t (1, 2). Other options include int, uint32_t, auto or using decltype on .size() of a container or the member type size_type.
This question may seem unsuitable as being opinion-based, but I don't think that's the case. Just because there isn't already a consensus on which type to use, doesn't mean that there cannot exist an objective answer. This is due to the fact that the different choices aren't only aesthetical, but can actually influence the behavior of the code.
For example, using an index variable type with mismatched signedness in the loop condition will cause compiler warnings, like this. Also, using a type that has a range that is too small can cause an overflow, which in the case of signed types is UB. At the same time, in some cases changing the loop counter type can cause "crazy performance deviations".
I also wanted to find out what is the most popular, though not necessarily the best, way to create for loop, so I used GitHub* search to find out. Here are the results:
Loop type
Code result count on GitHub (averaged; "manual" loop + range-based)
for (int
15.8m
for (size_t
11.6m
for (auto
7.5m
for (uint32_t
2.3m
std::for_each
501k
for (ptrdiff_t
98.7k
for (decltype
77.5k
There are certainly large differences in occurrence count between the different loop types, however, there doesn't seem to be a clear outstanding leader.
As such I post this question asking, what is the best type to use for the index variable in a for loop in C++ or what are the rules or conditions based on which this type should be chosen?
*: The GitHub search tool produces varying results for "code results" (count) each time, so I averaged 26 values. As the search is text-based it includes both results of the form for (int i = 0; i < n; ++i) and for (int i : vec).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm trying to find the fastest way to get the sum of a character orders in a string
for example if the string contains:
ABAACA;
Then the sum of character A will be:
(A=0)+(A=2)+(A=3)+(A=5)=10;
A=10;
i know some way to that but it takes too long,so would you please tell me how can i get the fastest sum?
Fastest way I see to do this with C++ (since there's nothing else describing your problem), involves parallel processing:
Parallel scan and in-place indexing
Reduction
Although it might not be what you were looking for.
Fastest non parallel solution:
-go over every char.
-increase count if match (if(ch=='A')count+=i;).
there is just no faster way, because you MUST visit each character.
Anyway if you have a working solution, it's probably the fastest already..
If no parallel tools are in place the fastest solution would be to just visit each char in loop and increase sum.
int count_match ( char* str, int length, char digit )
{
int output = 0;
for ( int index = 0; index < length; ++ index )
output += index & - (str[index] == digit);
return output;
}
If the length of the string could be known at compile time, then you could conceivably template that value out and let the compiler vectorize the loop for you.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm a newbee in C++ and I'm writing a C++ program that asks the user to input two integers and then it raises the first integer to the power specified by the second integer. For example, if the user enters 5 and 8, the result will be 5 subscript 8, i.e., number five will be raised to the eighth power. The program must not use any pre-defined C++ functions (like pow function) for this task. The program should allow the user to perform another calculation if they so desire. Can anyone help
I'm not going to give you any code, because that won't allow you to truly explore this concept. Rather, you should use this pseudo code to implement something on your own.
Create a function which accepts two inputs, the base and the exponent.
Now there are several ways to go about doing this. You can use efficient bit shifting, but let's start simple, shall we?
answer = base
i = 1
while i is less than or equal to exponent
answer = answer * base
return answer
Simply loop through multiplying the base by itself.
There are other ways that focus on efficiency. Look here to see something that you may want to attempt: are 2^n exponent calculations really less efficient than bit-shifts?
The program must not use any pre-defined C++ functions (like pow function) for this task
You can use some piece of c++ code like follows, to compute xy, without using any predefined function:
int x = 5;
int y = 3;
int result = 1;
for(int i = 0; i < y; ++i)
{
result *= x;
}
cout << result << endl;
Output:
125
See a working sample here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
My code is as follows:
double a,b; //These variable are inputs to the function
double *inArr; //This is also an iput to the function whose size is NumElements
double *arr = new double[numElements]; //NumElements is ~ 10^6
double sum = 0.0;
for(unsigned int i=0;i<numElements;++i)
{
double k = a*inArr[i] + b; //This doesn't take any time
double el = arr[i]; //This doesn't take any time
el *= k; //This doesn't take any time
sum += el; //This takes a long time!!!
}
This code goes over the elements of an array each time calculating a value k, for each element it adds k times that element to sum. I separated the code into so many steps so that when my profiler tells me which line takes a long time I will know exactly which calculation is the culprit. My profiler tells me that adding el to sum is what's slowing down my program (this might seem a little strange that a simple addition would be slow but I call this function hundreds of times and each time it performs millions of calculations). My only theory is that because sum is in a different scope operations using it take longer. So I edited the code to be:
double a,b; //These variable are inputs to the function
double *inArr; //This is also an iput to the function whose size is NumElements
double *arr = new double[numElements]; //NumElements is ~ 10^6
double sum = 0.0;
for(unsigned int i=0;i<numElements;++i)
{
double k = a*inArr[i] + b; //This doesn't take any time
double el = arr[i]; //This doesn't take any time
el *= k; //This doesn't take any time
double temp = sum + el; //This doesn't take any time
sum = el; //This takes a long time!!!
}
And now the sum operation takes very little time even though it accesses the sum variable. The assignment takes a long time now. Is my theory correct that the reason this happens is that it takes longer to assign to variables that aren't in the current scope? If so why is that true? Is there any way to make this assignment work quickly? I know I can optimize this using parallelization, I want to know if I can do any better serially. I am using VS 2012 running in release mode, I am using the VS performance analyzer as a profiler.
Edit:
Once I removed the optimization it turns out that the access to inArr is what is taking the most time.
Is my theory correct that the reason this happens is that it takes longer to assign to variables that aren't in the current scope?
No.
Your profiler is lying to you, and pinpointing the wrong source for the delay. Short of parallelisation this code cannot be optimised meaningfully without any knowledge of someQuickCalc. All the other operations are quite elementary.
There are limits to what a profiler can do. If you've compiled
with optimization, the compiler has probably rearranged a fair
bit of code, so the profiler can't necessarily tell which line
number is associated with any one particular instruction. And
both the compiler and the hardware will allow for a good deal of
overlapping; in many cases, the hardware will just go on to the
next instruction, even if the preceding one hasn't finished,
leaving a number of operations in the pipeline (and the compiler
will arrange the code so that the hardware can do this most
effectively). Thus, for example, the sub-expression inArr[i]
involves a memory access, which is probably significantly slower
than anything else. But the execution doesn't wait for it; the
execution doesn't wait until it actually needs the results. (If
the compiler is really clever, it may remark that arr[i]
accesses uninitialized memory, which is undefined behavior, so
it can skip the access, and give you any old random value.)
In your case, the compiler is probably only doing full
optimization within the loop, so the execution is only stalling
for the pipelined operations to finish when you write to
a variable outside the loop. And the profiler thus attributes
all of the time to this write.
(I've simplified greatly: for more details, I'd have to know
more about the actual processor, and look at the generated code
with and without optimization.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What I'm trying to do is fill the values of a specific matrix using unknown variables.
Here's the first implementation:
#define PHI(I,J,K) phi[xlength*ylength*(I)+xlength*(J)+(K)] //Macro that calls function
Where phi is a 1D matrix of dimensions xlength*ylength*tlength
or
phi= new double[xlength*ylength*tlength]; //code for phi
The other option is to define a function like
void PHI(double *&phi, int &I, int &J, int &K, double &value) //declare function
{
phi[xlength*ylength*I+xlength*J+K]=value; //return specific value of phi
}
I would use the macro or function in something like this:
for (int i=0;i<tlength;i++) //just making a loop here
{
for (int j=0;j<ylength;j++)
{
PHI(i,j,1)= stuff here //The macro or the function would go here
}
}
So what I'm doing is either using a macro to point to a specific cell of the matrix phi[] or I'm using a function to fill a specific value of the matrix phi[].
Which is faster?
Most likely no difference at all. The compiler will inline the function just as much as it inlines the macro. Since macros are much harder to use in a debugger, use a function.
And as I always say in case of "which performs better", you should always benchmark the different options, since differences in compilers can make some small difference in some cases (and in other cases make a big difference). Asking on SO or some other internet site will only tell you what other people think, not what actually happens in your real case.