Code executes slower after declaring variables with their respective types instead of double type
2 views (last 30 days)
Show older comments
Hello,
I'm working on a code which can be divided in two parts.
The first part consists of reading the input files from csv-files and storing them as variables.
The second part consists of manipulating those variables in order to get the different results.
Until recently all the variables were stored as double type. In order to reduce the execution time of the code we decided to store each variable with their most suitable type. For example the age of a person would be a uint8-type.
After I changed the code I made a performance test with a small part of the input files. Unfortunately, the change did not bring the performance improvement we expected, the change even slowed down the code.
All Double Most suited type
Part 1 30s 50s
Part 2 60s 80s
Total 90s 130s
This would mean the execution of the code with all the input files would take about 44% more time than previously from 35min run-time to 50 minute run-time.
Before the change the variables were read with the sscanf function from the csv-files. The result of the sscanf function is than stored as a double. (Because that's what sscanf does and there is no way to change it?)
After the change the variables are simply read as a string and then stored with their respective type (With the help of a hash-table, which gives the respective type of every variable).
Is there an explanation why the execution time of my code increased, even though I changed the data-types of the variables in order to free up memory space?
I would specifically like to know why the calculating part (Part 2) of my code has slowed down after the change. Because I did not change this part of the code the only thing that happened is the fact that the variables have different types and take less memory space.
Thank you very much,
Luca
0 Comments
Accepted Answer
David Young
on 29 Jan 2015
I'm sure that someone can offer a much more detailed and accurate story, but basically the machine is designed to be efficient with whole-word operations, typically manipulating 32 or 64 bits at once. If you operate on smaller units, such as 8-bit bytes, there is an overhead caused by packing and unpacking these. You are seeing one example of a trade-off, in which you save on memory but lose on speed.
0 Comments
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!