Skip to content

- Improvements on get method performance#14

Closed
sergiosvieira wants to merge 1 commit intouestla:masterfrom
sergiosvieira:master
Closed

- Improvements on get method performance#14
sergiosvieira wants to merge 1 commit intouestla:masterfrom
sergiosvieira:master

Conversation

@sergiosvieira
Copy link
Copy Markdown

Hi,

I get some improvements on get access method performance.
In my tests, I used a 1000x1000 matrix and I have made two loops to access each element.

void testInternalAccess(void)
{
SparseMatrix matrix(1000, 1000);
fillRandomMatrix(matrix);
high_resolution_clock::time_point t1 = high_resolution_clock::now();
for (int row = 1; row <= matrix.getRowCount(); ++row)
{
for (int col = 1; col <= matrix.getColumnCount(); ++col)
{
matrix.get(row, col);
}
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration timeSpan = duration_cast<duration>(t2 - t1);
std::cout << "Access matrix values " << timeSpan.count() << " seconds.\n";
}

Test Results

From 5.64532 to 1.22307 seconds.

@uestla
Copy link
Copy Markdown
Owner

uestla commented Feb 16, 2018

Thank you, that is an interesting insight!

Looks like the method std::vector::at() is terribly slow and just replacing it with operator[] speeded up your test ~10 times on my machine.

The main difference between those two approaches is that the operator[] does not check for out-of-range indexes - that is no problem in our case because we're always checking that given coordinates are valid (method validateCoordinates()).

And because the slow method at() is being called almost everywhere, replacing it with [] access speeded up every operation!

If you won't mind, I won't merge directly your changes here but commit my own replacement and mention you in the commit message ;-)

@sergiosvieira
Copy link
Copy Markdown
Author

sergiosvieira commented Feb 16, 2018 via email

@uestla uestla closed this in d62a0c2 Feb 17, 2018
uestla added a commit that referenced this pull request Feb 17, 2018
This replaces calling std::vector::at() with direct operator[] access
Out-of-range access is prevented thanks to validateCoordinates() method
uestla added a commit that referenced this pull request Feb 17, 2018
This replaces calling std::vector::at() with direct operator[] access.
It is somehow faster - maybe compiler-related optimization or lack of
out-of-range checking when using operator[].
Out-of-range access is prevented thanks to validateCoordinates() method
uestla added a commit that referenced this pull request Feb 17, 2018
This replaces calling std::vector::at() with direct operator[] access.
It is somehow faster - maybe compiler-related optimization or lack of out-of-range checking when using operator[].
Out-of-range access is prevented thanks to validateCoordinates() method
uestla added a commit that referenced this pull request Feb 22, 2018
This replaces calling std::vector::at() with direct operator[] access.
It is somehow faster - maybe compiler-related optimization or lack of out-of-range checking when using operator[].
Out-of-range access is prevented thanks to validateCoordinates() method
uestla added a commit that referenced this pull request Feb 22, 2018
This replaces calling std::vector::at() with direct operator[] access.
It is somehow faster - maybe compiler-related optimization or lack of out-of-range checking when using operator[].
Out-of-range access is prevented thanks to validateCoordinates() method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants