-
-
Save asparc/c6597c70b43a493cca1428af77f9304c to your computer and use it in GitHub Desktop.
Timings of passing function arguments in Matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pass_function_arguments() | |
global sz, global loops | |
sz = 10000; | |
loops = 5; | |
dummy_mat = rand(sz); | |
dummy_class = DataClass(); | |
dummy_class.data = dummy_mat; | |
dummy_handle = DataClassHandle(); | |
dummy_handle.data = dummy_mat; | |
dummy_struct = struct; | |
dummy_struct.data = dummy_mat; | |
disp(" ") | |
disp("Read") | |
disp("====") | |
fprintf('ref: %f\n', timer(dummy_mat, [], false, false)); | |
fprintf('ref: %f\n', timer(dummy_mat, [], false, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @read, false, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @read, false, false)); | |
fprintf('struct: %f\n', timer(dummy_struct, @read, false, true)); | |
fprintf('struct: %f\n', timer(dummy_struct, @read, false, true)); | |
fprintf('class: %f\n', timer(dummy_class, @read, false, true)); | |
fprintf('class: %f\n', timer(dummy_class, @read, false, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @read, false, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @read, false, true)); | |
disp(" ") | |
disp("Write") | |
disp("=====") | |
fprintf('ref: %f\n', timer(dummy_mat, [], true, false)); | |
fprintf('ref: %f\n', timer(dummy_mat, [], true, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @write, false, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @write, false, false)); | |
fprintf('struct: %f\n', timer(dummy_struct, @write, false, true)); | |
fprintf('struct: %f\n', timer(dummy_struct, @write, false, true)); | |
fprintf('class: %f\n', timer(dummy_class, @write, false, true)); | |
fprintf('class: %f\n', timer(dummy_class, @write, false, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @write, false, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @write, false, true)); | |
disp(" ") | |
disp("Write & re-assign") | |
disp("=================") | |
fprintf('ref: %f\n', timer(dummy_mat, [], true, false)); | |
fprintf('ref: %f\n', timer(dummy_mat, [], true, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @write, true, false)); | |
fprintf('mat: %f\n', timer(dummy_mat, @write, true, false)); | |
fprintf('struct: %f\n', timer(dummy_struct, @write, true, true)); | |
fprintf('struct: %f\n', timer(dummy_struct, @write, true, true)); | |
fprintf('class: %f\n', timer(dummy_class, @write, true, true)); | |
fprintf('class: %f\n', timer(dummy_class, @write, true, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @write, true, true)); | |
fprintf('handle: %f\n', timer(dummy_handle, @write, true, true)); | |
end | |
function t = timer(dummy, func, do_reassign, has_data_field) | |
global sz, global loops | |
tic; | |
for i = 1:loops | |
if do_reassign | |
if isempty(func) | |
dummy(randi(sz), randi(sz)) = rand; % ref write | |
else | |
dummy = func(dummy, has_data_field); | |
end | |
else | |
if isempty(func) | |
foo = dummy(randi(sz), randi(sz)); % ref read | |
else | |
func(dummy, has_data_field); | |
end | |
end | |
end | |
t = toc; | |
end | |
function read(dummy, has_data_field) | |
global sz | |
if has_data_field | |
foo = dummy.data(randi(sz), randi(sz)); | |
else | |
foo = dummy(randi(sz), randi(sz)); | |
end | |
end | |
function dummy = write(dummy, has_data_field) | |
global sz | |
if has_data_field | |
dummy.data(randi(sz), randi(sz)) = rand; | |
else | |
dummy(randi(sz), randi(sz)) = rand; | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
classdef DataClass | |
properties | |
data | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
classdef DataClassHandle < handle | |
properties | |
data | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Read | |
==== | |
ref: 0.000815 | |
ref: 0.000517 | |
mat: 0.001349 | |
mat: 0.000251 | |
struct: 0.001657 | |
struct: 0.000061 | |
class: 0.001487 | |
class: 0.000059 | |
handle: 0.001249 | |
handle: 0.000055 | |
Write | |
===== | |
ref: 0.410640 | |
ref: 0.417686 | |
mat: 2.432822 | |
mat: 2.373602 | |
struct: 2.339701 | |
struct: 2.372296 | |
class: 2.333061 | |
class: 2.352433 | |
handle: 0.425259 | |
handle: 0.000089 | |
Write & re-assign | |
================= | |
ref: 0.410176 | |
ref: 0.412665 | |
mat: 0.414462 | |
mat: 0.411518 | |
struct: 0.419934 | |
struct: 0.412815 | |
class: 0.414880 | |
class: 0.411838 | |
handle: 0.000737 | |
handle: 0.000062 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment