954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

efficient array update

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.

a10m84
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

I'll wager a bet that he means "The most efficient" means by which to add a value to all items in an array.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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};

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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. ;)

grumpier
Posting Whiz in Training
211 posts since Aug 2008
Reputation Points: 193
Solved Threads: 32
 

LOL

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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.

a10m84
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

[] 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.

kbshibukumar
Junior Poster in Training
65 posts since Jan 2009
Reputation Points: 12
Solved Threads: 8
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You