This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster. - for (int i = 0; i < 50; i++)
-
{
-
a[i] = getNumber(i);
-
}
Right now it takes around 40-50 seconds to run.
I've already tried loop unrolling, but that didn't optimize it at all.
Loop unrolling: - for (int i = 0; i < 50; i += 5)
-
{
-
a[i] = getNumber(i);
-
a[i+1] = getNumber(i+1);
-
a[i+2] = getNumber(i+2);
-
a[i+3] = getNumber(i+3);
-
a[i+4] = getNumber(i+4);
-
}
I'm still new to C++ and out of practice at the moment, I'm sure theres a method out there that could either optimize or replace my recursion but do the same thing, could anyone care to enlighten me?
Thanks.
11 1623
This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster. - for (int i = 0; i < 50; i++)
-
{
-
a[i] = getNumber(i);
-
}
Right now it takes around 40-50 seconds to run.
I've already tried loop unrolling, but that didn't optimize it at all.
Loop unrolling: - for (int i = 0; i < 50; i += 5)
-
{
-
a[i] = getNumber(i);
-
a[i+1] = getNumber(i+1);
-
a[i+2] = getNumber(i+2);
-
a[i+3] = getNumber(i+3);
-
a[i+4] = getNumber(i+4);
-
}
I'm still new to C++ and out of practice at the moment, I'm sure theres a method out there that could either optimize or replace my recursion but do the same thing, could anyone care to enlighten me?
Thanks.
HI,
Themproblem is not with the for loop.
The problem is with the function getNumber().
Use time function to calculate the time taken by the getNumber() method and then try to optimize the function
Raghuram
I do have a timer that calculates how long the function takes, and its roughly 40-50 seconds like I said, and I haven't been able to do anything to optimize it just yet, is there any method to optimize the extraction/storage of the variables from the function to the array?
Well, if you are working in c++ you can return reference, this will avoid unnecessary copying from function to your array.
Savage
I'm still new to reference returns and pointers in general, how would I be able to imitate what the for-loop is doing using other methods that would be much more speedy?
I was thinking of something like:
1. Run function (somewhere outside of a loop)
2. Point to array
3. Fill space in array with number
...
why cant you try this code
for( i= 50 ; i > 0 ; i--- )
{
// code
}
why I said this would when ever you say in the for loop i = 0 ..Zero flag gets initialised ..
that also consumes some extra time..
In turn my suggestion is instead of having a incremental loop use a decremental loop..
just check whether it works for you or not..
let me know about the result
Interesting suggestion, I just ran it, its still taking some time to load...
it took 20 seconds more than my last attempt, and it ended with an error :(
I think the point of the exercise is to replace the recursion with something else...i.e. not a recursion? like I said I'm new to pointers and beyond so if anyone has an idea of how to tackle this please let us know.
(continues to attempt the problem)
edit: the solution given was a runtime of 0 seconds btw. so supposedly this "new" method should reduce run time completely, not just optimize it slightly.
Interesting suggestion, I just ran it, its still taking some time to load...
it took 20 seconds more than my last attempt, and it ended with an error :(
I think the point of the exercise is to replace the recursion with something else...i.e. not a recursion? like I said I'm new to pointers and beyond so if anyone has an idea of how to tackle this please let us know.
(continues to attempt the problem)
edit: the solution given was a runtime of 0 seconds btw. so supposedly this "new" method should reduce run time completely, not just optimize it slightly.
Can you replace getNumber with something like getArray?
Savage
This is actaully a really simple recursion (for-loop), but I'm having some trouble optimizing it so that it would run faster.
I don't understand why this is recursive - for (int i = 0; i < 50; i++)
-
{
-
a[i] = getNumber(i);
-
}
Right now it takes around 40-50 seconds to run.
50 iterations shouldn't take very long. If you took out the "getNumber" function but stilled timed it, the time should be less than 1 second. I'm guessing it takes your getNumber function about a second to complete. I would suggest analysing that function instead of the for-loop.
If you are having problems with pointers, try looking at these web resources. Tutorial on Pointers Tutorial on Pointers WITH PICTURES!!! :)
Pointers are good when you have many copies of a variable - but the copies will all stay the same. Instead of changing many copies, just have them point to the variable instead and change the variable instead.
A good example of a point is URL links. You give the location on where to go. Imagine if you couldn't give the location but had to store the page itself. If that page changed, you have to change ALL the locations where the page is stored. I hope that was a good analogy to explain why pointers can be quicker.
In turn my suggestion is instead of having a incremental loop use a decremental loop..
just check whether it works for you or not..
let me know about the result
Don't do that; if a processor prefetched pages from memory (after a cache miss)
it fetches them in a forward direction. Your suggestion kills the number of cache
hits big times.
kind regards,
Jos
@ kreagon
The exercise only allows me to change the for-loop, or delete it and put in something new, editing the function is out of my power :(
ty for the links.
Can you look at what getNumber(i) does and somehow replace it with hard-code here? I came to the same conclusion that getNumber is what is slowing you down, so the way to speed this code snippet up is to remove that function.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: A Future Computer Scientist |
last post by:
A question: Is it really important to think about optimizing the native code
or optimizing it for P Code? Or does the code you write make a difference?
|
by: BlackHawke |
last post by:
My Name is Nick Soutter, I am the owner of a small game company, Aepox Games
(We're in the middle of a name change from "Lamar Games"),
www.lamargames.net.
Our first commercial game,...
|
by: deko |
last post by:
I have an mdb (2002 file format) that uses memo fields extensively. I've
read a lot about how problematic memo fields can be and that avoiding them
is a good idea. But I'm stuck with them and was...
|
by: macluvitch |
last post by:
Hello folks,
During developping I've met a problem this is how it looks like
I have an expression like this
(type *)var + 1
Wath heppens is I want to make a pointer to this
|
by: Brian |
last post by:
In particular, this question goes out to the Microsoft C++ development
team. Back in June, Ronald Laeremans posted the following message. Has
the optimizing compiler been included with the...
|
by: Flashman |
last post by:
A little confusing with setting up optimizing options with 2003 .NET.
Under the Optimization Tab. if you set to /O1 or /O2 is the program ignoring
the settings for Inline Function expansion,...
|
by: Rico |
last post by:
Hello,
I have an application that I'm converting to Access 2003 and SQL Server 2005
Express. The application uses extensive use of DAO and the SEEK method on
indexes. I'm having an issue when...
|
by: Bern McCarty |
last post by:
I have a simple ref class in its own namespace that needs to coexist with
a legacy typedef alias for "unsigned int" in the global namespace that has
the identifier as itself. Everything compiles...
|
by: Miguel Perez |
last post by:
Please critique this tail call optimizing decorator I've written. I've
tried to fix the pitfalls of other proposed decorators, and the result
is this one that supports mutual recursion, does not...
|
by: copx |
last post by:
I am writing a program which uses sqrt() a lot. A historically very slow
function but I read on CPUs with SSE support this is actually fast. Problem:
C's sqrt() (unlike C++ sqrt()) is defined to...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |