필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by