Info

This question is closed. Reopen it to edit or answer.

How to speed up program when modifying large amounts of data inside functions?

1 view (last 30 days)
I am running a large simulation on a graph. Each of the vertices has several traits I need to keep track of and sometimes change throughout the simulation. I'm storing this in a struct array called Vertices. The program runs fine until I call a function which takes Vertices as an input, modifies traits of some of the vertices, and spits vertices back out. The profiler is showing that the most time spent is on the first time the function modifies Vertices, and on the end statement of the function. I assume that this has something to do with the fact that the entire struct Vertices is being copied at these points. Anyone have any more specific ideas about what is going on here? Thanks.
  1 Comment
Adam
Adam on 26 Feb 2016
Edited: Adam on 26 Feb 2016
I am not too familiar with structs and exactly what they do with respect to copy semantics in different situations. Have you tried using a class instead of a struct? In particular, if you are just editing only certain parts of a the structure than a class derived from 'handle' would allow you to pass it around by reference rather than by value.
e.g.
classdef myClass < handle
properties
prop1 = 7;
prop2 = 17;
prop3 = 'a';
prop4 = [];
end
end
where prop1, etc would be equivalent to the fields you have on your structure.
I don't know for sure this would be faster as, like I said, I am not familiar with struct behaviour, but it is something that ought to be quick to test for speed as the syntax of a class matches that of a struct in a simple case like this.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!