Two Loops versus One Combined Performance Time?
15 次查看(过去 30 天)
显示 更早的评论
Hello, I got a very big file that I need to process, and the current script works with for loops for each variable. For example:
for i = 1:10
% do some string mapping on two unsorted arrays.
end
for i = 1:10
% do some more string mapping on two unsorted arrays.
end
My question is would this be faster than the above?:
for i = 1:10
% do some string mapping on two unsorted arrays.
% do some more string mapping on two unsorted arrays.
end
Please don't say try it, and see it for yourself. I am looking for sort of a theoretical answer, not a practical one. I would have tested it if I could. Difference might be minimal, but I got 50 million iterations to run through, so yes anything is appreciated.
EDIT Changed do sth to do string mapping on an unsorted array.
2 个评论
Guillaume
2018-9-11
Doing something once is usually faster than doing it twice, so I don't really understand why you even ask the question.
回答(2 个)
Steven Lord
2018-9-11
It depends a lot on what "do sth" and "do sth else" are.
Picture "do sth" as "if the file doesn't exist, create it" and "do sth else" as "if the file exists, delete it." In that case, the first approach creates the file once and deletes it once. The second approach creates and deletes the file ten times.
If on the other hand "do sth" were "x = 1:10;" and "do sth else" were "y = 1:10;", those are probably going to take the same (or a very similar) amount of time.
So the theoretical answer is "It depends." The practical answer is "Try it several times with smaller data sets and/or fewer iterations than 50 million, and use that information to explore how the specific operations you're performing behave."
2 个评论
Steven Lord
2018-9-11
There's still not enough information here, and short of posting your code I don't think you will be able to provide enough information to receive an answer more specific than "it depends".
Exactly what you mean by "map a string array to a bigger string array", how you've implemented it (which functions and/or objects you're using), whether you're using a cell array of char vectors or a string array, all these and many other details may be relevant to how your code performs.
That leaves out completely the question of whether one or more of your for loops is suitable to be converted into a parfor loop, which may make the multiple loops more attractive (if some but not all of the loops can be executed in parallel, putting all your computations in one loop could prevent it from being run in parallel using parfor.)
If you're looking for advice on how to measure the performance of your code and identify potential efficiency improvements, I recommend reading through this section in the documentation.
OCDER
2018-9-11
"Please don't say try it, and see it for yourself. "
Try it and see for yourself.
"I am looking for sort of a theoretical answer, not a practical one."
Why? In theory, a single loop is faster than a double loop. In practice, theory is not always right.
"I would have tested it if I could."
Perhaps use a simpler test case...
a = rand(1, 1000000);
tic
for j = 1:length(a)
b = sin(a(j));
end
for j= 1:length(a)
c = cos(a(j));
end
toc %.050427 sec
tic
for j = 1:length(a)
b = sin(a(j));
c = cos(a(j));
end
toc %0.041288 sec
" there is an explanation based on computer science principles that I want to learn."
That's too hard to predict... You need to understand JIT compilers which is the execution engine behind Matlab. There is a small overhead for starting a new for loop.
tic
for j = 1:length(a)
for k = 1
end
end
toc %0.938168 sec
tic
for j = 1:length(a)
end
toc %0.023095 sec
Here's a good article to get started on code optimization.
2 个评论
OCDER
2018-9-11
In science, theory and fact are different words. A synonym for theory is speculation, and as you said correctly "prediction". It is not guaranteed to work out in practice. The miracle drug should work in theory, but it failed (and the stock market prices drops 40%...)
"I bet if you run the same code 3-4 times you will see what I mean."
We have been encouraging YOU to do it. We don't know what you want to do exactly. Your question is not clear. You have 3 people, all answering, all not satisfactory. It means your question needs more details.
Perhaps spend more time showing more code about what you want to do, instead of side tracking us with the speed of 2 for loops.
"...do string mapping on an unsorted array."
Looks like you want a mapping function? If so, I think JAVA's hashmap would work. Matlab has a containers.Map, but seems like people are having speed issues.
For JAVA util hashmap tests, see this:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!