943,771 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1043
  • C++ RSS
Jan 14th, 2009
0

efficient array update

Expand Post »
Hello , I am developing an application in realtime and I need to update some values in an array. What is the best method to add a value to every position of an array?

Thank you for your help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
a10m84 is offline Offline
2 posts
since Jan 2009
Jan 14th, 2009
1

Re: efficient array update

You're developing a realtime application, and don't know how to add values to elements of an array?????

Pull my other leg: it'll play a tune for you.

What is the criterion by which you would describe a method as "best"? If you are truly designing a realtime application, you will realise the significance of that question.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 14th, 2009
0

Re: efficient array update

I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 14th, 2009
0

Re: efficient array update

If you want to set all elements to 0 then you can use memset(). Microsoft compilers implement that function in assembly because the chip has an instruction that will do it, and that is about as efficient as you can get. I don't know how other compilers implement it.

Also use the = {0} when declaring the array. int myarray[255] = {0};
Last edited by Ancient Dragon; Jan 14th, 2009 at 9:48 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jan 14th, 2009
0

Re: efficient array update

Click to Expand / Collapse  Quote originally posted by Comatose ...
I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.
I'd be betting on your side. But that just means he then needs to define the criterion for "The most efficient". For example, does it mean "fastest", "smallest number of machine instructions", "least memory consumption", "least heat output from the CPU"

He probably also needs to specify the type of array elements. Adding a value to elements of an array of strings might conceivably require a technique that differs from adding a value to elements of an array of int.

Incidentally, I despise irrelevant questions that put carts before horses.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 14th, 2009
0

Re: efficient array update

LOL
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 15th, 2009
0

Re: efficient array update

Click to Expand / Collapse  Quote originally posted by grumpier ...
You're developing a realtime application, and don't know how to add values to elements of an array?????

Pull my other leg: it'll play a tune for you.

What is the criterion by which you would describe a method as "best"? If you are truly designing a realtime application, you will realise the significance of that question.
I know how to add values to an array. I am developing an application with RTAI, comedi and Qwt (plotting in realtime). Right know the application works ok but I need to improve efficiency. I need to add values (not setting to 0, add value to shift the coordinates) to an array of double (the x values). I need to do it in an efficient manner.

Thank you for your help.
Last edited by a10m84; Jan 15th, 2009 at 5:21 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
a10m84 is offline Offline
2 posts
since Jan 2009
Jan 15th, 2009
0

Re: efficient array update

[] operator is the best to add values to an array, I think. Applicable only if you know the index in advance.
If you have to search in runtime for the position to which we can add a value, it will have a complexity of O(n). In that situation, if possible, use stl map.
Reputation Points: 12
Solved Threads: 8
Junior Poster in Training
kbshibukumar is offline Offline
65 posts
since Jan 2009
Jan 15th, 2009
0

Re: efficient array update

> I know how to add values to an array. I am developing an application
> with RTAI, comedi and Qwt (plotting in realtime).
Oh please tell me you've used a profiler to work out where the real hot spots are, and not just picked random lines of code out based on guesswork.

Dickering about with an array of a few 1000 entries is nothing to the cost of plotting all those millions of pixels in your nice glossy user interface.

You just can't say "draw it" and hope that it takes zero time, because it won't. How you draw it will have a much larger impact on performance than what you're suggesting.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: About Monthly Calendar
Next Thread in C++ Forum Timeline: using c++ file handling write a salary slip





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC