フィルターのクリア

Working with global variables is fast?

2 ビュー (過去 30 日間)
Volkan Kandemir
Volkan Kandemir 2012 年 3 月 28 日
コメント済み: James Tursa 2018 年 1 月 25 日
Here is my problem I am goint write a large program and at the begining I want to decide the way that I will code in order to run my program fast.
In my program there will be a huge martix and transfering it from functions to functions may be time consuming. Hence is it posible to work with functions on main variable with out copying it on memory. I try to declerate global variable but nothing changes since functions are copying it on the memory too.
In fact, what I would like to get is instead of trasnfering huge data to functions and getting my data back, let the data to be stationary while functions work on it.
Here is an example to compare the running time. Please be awere of that this is just an example to get running time.
Resaults are:
Elapsed time is 0.000088 seconds. (running it on main function)
Elapsed time is 0.000767 seconds. (transfering data to functions)
Elapsed time is 0.000753 seconds. (using a global variable)
Any suggestions? ----------------------------------------------
function main
clear;
clear global;
clc;
a=1:10000;
tic
for i=1:10000
a(i)=a(i)+1;
end
toc
a=1:10000;
tic
a=nonglobalfunction(a);
toc
global A
A=1:10000;
tic
globalfunction()
toc
end
function globalfunction
global A
for i=1:10000
A(i)=A(i)+1;
end
end
function a=nonglobalfunction(a)
for i=1:10000
a(i)=a(i)+1;
end
end
  1 件のコメント
Geoff
Geoff 2012 年 3 月 30 日
By the way, you are testing such tiny execution times that it's hard to get a meaningful result. The usual approach here is to repeat each test maybe 5000 times and measure the entire loop. Divide the resulting time by 5000 and you'll have a metric that is useful for comparison.

サインインしてコメントする。

回答 (2 件)

per isakson
per isakson 2012 年 3 月 29 日
Read Lorens blog on in-place functions
  1 件のコメント
James Tursa
James Tursa 2018 年 1 月 25 日
Another related option that was just posted to the FEX for getting shared data copies of contiguous sub-sections of an existing variable:

サインインしてコメントする。


Volkan Kandemir
Volkan Kandemir 2012 年 3 月 29 日
Thank you but I have already used inplace functions in my code. In functions I am not declerating a new variable. When I create a new variable running time will increase from 0.000720 seconds to 0.002741 seconds.
Using a pointer may be helpfull but I have never declerate a variable as a pointer on MATLAB. I am going to resarch on that but still if you have any suggestions please write.
  3 件のコメント
Geoff
Geoff 2012 年 3 月 30 日
Have you considered putting your information into a class and just pass around a handle to your object? I haven't used classes in MatLab, but (correct me if I'm wrong) I understand that the object's handle is effectively a pointer, and passing around an object this way will not result in it being copied.
Volkan Kandemir
Volkan Kandemir 2012 年 3 月 30 日
Thank you Per :)
Hi Geoff,
I haven't used classes in MatLab too :( but I have used classes on C#. I will make a resarch on that. Thank you for your suggestion.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by