Is there a better way to do run time analysis than this? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election ResultsIs there a straightforward way to run pandas.DataFrame.isin in parallel?TF-IDF vectorizer doesn't work better than countvectorizerBetter way to break out a date?When does decision tree perform better than the neural network?Multivariate VAR model: ValueError: x already contains a constantgpu pytorch code way slower than cpu code?Analysis of Time Series dataWhich different visualizations to make for time series analysis?tensorflow: is there a way to specify XLA_GPU with tensorflow?
Does silver oxide react with hydrogen sulfide?
"klopfte jemand" or "jemand klopfte"?
AppleTVs create a chatty alternate WiFi network
Understanding p-Values using an example
In musical terms, what properties are varied by the human voice to produce different words / syllables?
Differences to CCompactSize and CVarInt
Show current row "win streak"
Resize vertical bars (absolute-value symbols)
Can an iPhone 7 be made to function as a NFC Tag?
How to change the tick of the color bar legend to black
Relating to the President and obstruction, were Mueller's conclusions preordained?
Printing attributes of selection in ArcPy?
License to disallow distribution in closed source software, but allow exceptions made by owner?
Would color changing eyes affect vision?
What are the main differences between Stargate SG-1 cuts?
I can't produce songs
My mentor says to set image to Fine instead of RAW — how is this different from JPG?
How many time has Arya actually used Needle?
One-one communication
Test print coming out spongy
Google .dev domain strangely redirects to https
What adaptations would allow standard fantasy dwarves to survive in the desert?
Universal covering space of the real projective line?
Asymptotics question
Is there a better way to do run time analysis than this?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election ResultsIs there a straightforward way to run pandas.DataFrame.isin in parallel?TF-IDF vectorizer doesn't work better than countvectorizerBetter way to break out a date?When does decision tree perform better than the neural network?Multivariate VAR model: ValueError: x already contains a constantgpu pytorch code way slower than cpu code?Analysis of Time Series dataWhich different visualizations to make for time series analysis?tensorflow: is there a way to specify XLA_GPU with tensorflow?
$begingroup$
I currently have 2 different functions with options to vectorise them: acc_rej_sine(max_iter, algorithm=None)
and analytical_sine(max_iter, algorithm=None)
for which I'm trying to compare their run time against the number of iterations computed. i.e. compare all 4 methods; 2 looped, 2 vectorised. Essentially my code goes something like this:
def analytical_sine(max_iter, algorithm=None):
if algorithm is None:
count = 0
analytical_hist = []
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
uni_dist = np.random.uniform(0, 1)
arccos = np.arccos(1 - 2*uni_dist)
analytical_hist.append(arccos)
elif algorithm is "vectorise":
analytical_hist = np.arccos(1 - 2*np.random.uniform(0, 1, max_iter))
return analytical_hist
def acc_rej_sine(max_iter, algorithm=None):
x = np.random.uniform(0, np.pi, max_iter)
y = np.random.rand(max_iter)
if algorithm is None:
accepted_x = []
j = count = 0
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
if y[i] <= np.sin(x[i]):
accepted_x.append(x[i])
j += 1
elif algorithm is "vectorise":
accepted_x = np.extract((y <= np.sin(x)), x)
return accepted_x
def runtime(func, runs, max_iter, algorithm=None):
time = []
for i in range(runs):
start = timer()
func()
end = timer()
time.append((end-start))
error = np.std(time)
time = sum(time)/runs
return time, error
def time_analysis():
time1, time2, time3, time4 = [], [], [], []
error1, error2, error3, error4 = [], [], [], []
for i in np.arange(1, 8, 1):
max_iter = 10**i
time, error = runtime(analytical_sine, 5, int(max_iter))
time1.append(time)
error1.append(error)
time, error = runtime(analytical_sine, 5, int(max_iter), "vectorise")
time2.append(time)
error2.append(error)
time, error = runtime(acc_rej_sine, 5, int(max_iter))
time3.append(time)
error3.append(error)
time, error = runtime(acc_rej_sine(max_iter), 5, int(max_iter), "vectorise")
time4.append(time)
error4.append(error)
return [time1, time2, time3, time4], [error1, error2, error3, error4]
# to run the code I would probably do something like this
time, error = time_analysis()
#then if I wanna plot number of iterations vs run time with errors I would probably do something along the lines of
plt.plot(max_iter, time) # max_iter would be a list of [10**i for i in np.arange(1, 8, 1)]
plt.errorbar(error)
So the idea is my runtime()
function would allow me to put in any of the 4 functions that I want to compare(which currently still isn't working yet and I can't/haven't figured out why), and run it for 5 times, work out the mean run time and standard deviation as the error. In return my time_analysis()
function would run runtime()
for different functions for different max_iter
(max iterations) which goes like [10, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7]
so I can plot max iterations against run time. However, this whole method seems quite cumbersome and inelegant, as my time_analysis()
requires me to repeatedly work out time and error and append it to a list. Is there a better way of timing this?(Also my runtime()
doesn't work yet lol because my algorithm
argument seems to be making something not callable
)
python data-analysis
$endgroup$
add a comment |
$begingroup$
I currently have 2 different functions with options to vectorise them: acc_rej_sine(max_iter, algorithm=None)
and analytical_sine(max_iter, algorithm=None)
for which I'm trying to compare their run time against the number of iterations computed. i.e. compare all 4 methods; 2 looped, 2 vectorised. Essentially my code goes something like this:
def analytical_sine(max_iter, algorithm=None):
if algorithm is None:
count = 0
analytical_hist = []
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
uni_dist = np.random.uniform(0, 1)
arccos = np.arccos(1 - 2*uni_dist)
analytical_hist.append(arccos)
elif algorithm is "vectorise":
analytical_hist = np.arccos(1 - 2*np.random.uniform(0, 1, max_iter))
return analytical_hist
def acc_rej_sine(max_iter, algorithm=None):
x = np.random.uniform(0, np.pi, max_iter)
y = np.random.rand(max_iter)
if algorithm is None:
accepted_x = []
j = count = 0
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
if y[i] <= np.sin(x[i]):
accepted_x.append(x[i])
j += 1
elif algorithm is "vectorise":
accepted_x = np.extract((y <= np.sin(x)), x)
return accepted_x
def runtime(func, runs, max_iter, algorithm=None):
time = []
for i in range(runs):
start = timer()
func()
end = timer()
time.append((end-start))
error = np.std(time)
time = sum(time)/runs
return time, error
def time_analysis():
time1, time2, time3, time4 = [], [], [], []
error1, error2, error3, error4 = [], [], [], []
for i in np.arange(1, 8, 1):
max_iter = 10**i
time, error = runtime(analytical_sine, 5, int(max_iter))
time1.append(time)
error1.append(error)
time, error = runtime(analytical_sine, 5, int(max_iter), "vectorise")
time2.append(time)
error2.append(error)
time, error = runtime(acc_rej_sine, 5, int(max_iter))
time3.append(time)
error3.append(error)
time, error = runtime(acc_rej_sine(max_iter), 5, int(max_iter), "vectorise")
time4.append(time)
error4.append(error)
return [time1, time2, time3, time4], [error1, error2, error3, error4]
# to run the code I would probably do something like this
time, error = time_analysis()
#then if I wanna plot number of iterations vs run time with errors I would probably do something along the lines of
plt.plot(max_iter, time) # max_iter would be a list of [10**i for i in np.arange(1, 8, 1)]
plt.errorbar(error)
So the idea is my runtime()
function would allow me to put in any of the 4 functions that I want to compare(which currently still isn't working yet and I can't/haven't figured out why), and run it for 5 times, work out the mean run time and standard deviation as the error. In return my time_analysis()
function would run runtime()
for different functions for different max_iter
(max iterations) which goes like [10, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7]
so I can plot max iterations against run time. However, this whole method seems quite cumbersome and inelegant, as my time_analysis()
requires me to repeatedly work out time and error and append it to a list. Is there a better way of timing this?(Also my runtime()
doesn't work yet lol because my algorithm
argument seems to be making something not callable
)
python data-analysis
$endgroup$
add a comment |
$begingroup$
I currently have 2 different functions with options to vectorise them: acc_rej_sine(max_iter, algorithm=None)
and analytical_sine(max_iter, algorithm=None)
for which I'm trying to compare their run time against the number of iterations computed. i.e. compare all 4 methods; 2 looped, 2 vectorised. Essentially my code goes something like this:
def analytical_sine(max_iter, algorithm=None):
if algorithm is None:
count = 0
analytical_hist = []
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
uni_dist = np.random.uniform(0, 1)
arccos = np.arccos(1 - 2*uni_dist)
analytical_hist.append(arccos)
elif algorithm is "vectorise":
analytical_hist = np.arccos(1 - 2*np.random.uniform(0, 1, max_iter))
return analytical_hist
def acc_rej_sine(max_iter, algorithm=None):
x = np.random.uniform(0, np.pi, max_iter)
y = np.random.rand(max_iter)
if algorithm is None:
accepted_x = []
j = count = 0
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
if y[i] <= np.sin(x[i]):
accepted_x.append(x[i])
j += 1
elif algorithm is "vectorise":
accepted_x = np.extract((y <= np.sin(x)), x)
return accepted_x
def runtime(func, runs, max_iter, algorithm=None):
time = []
for i in range(runs):
start = timer()
func()
end = timer()
time.append((end-start))
error = np.std(time)
time = sum(time)/runs
return time, error
def time_analysis():
time1, time2, time3, time4 = [], [], [], []
error1, error2, error3, error4 = [], [], [], []
for i in np.arange(1, 8, 1):
max_iter = 10**i
time, error = runtime(analytical_sine, 5, int(max_iter))
time1.append(time)
error1.append(error)
time, error = runtime(analytical_sine, 5, int(max_iter), "vectorise")
time2.append(time)
error2.append(error)
time, error = runtime(acc_rej_sine, 5, int(max_iter))
time3.append(time)
error3.append(error)
time, error = runtime(acc_rej_sine(max_iter), 5, int(max_iter), "vectorise")
time4.append(time)
error4.append(error)
return [time1, time2, time3, time4], [error1, error2, error3, error4]
# to run the code I would probably do something like this
time, error = time_analysis()
#then if I wanna plot number of iterations vs run time with errors I would probably do something along the lines of
plt.plot(max_iter, time) # max_iter would be a list of [10**i for i in np.arange(1, 8, 1)]
plt.errorbar(error)
So the idea is my runtime()
function would allow me to put in any of the 4 functions that I want to compare(which currently still isn't working yet and I can't/haven't figured out why), and run it for 5 times, work out the mean run time and standard deviation as the error. In return my time_analysis()
function would run runtime()
for different functions for different max_iter
(max iterations) which goes like [10, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7]
so I can plot max iterations against run time. However, this whole method seems quite cumbersome and inelegant, as my time_analysis()
requires me to repeatedly work out time and error and append it to a list. Is there a better way of timing this?(Also my runtime()
doesn't work yet lol because my algorithm
argument seems to be making something not callable
)
python data-analysis
$endgroup$
I currently have 2 different functions with options to vectorise them: acc_rej_sine(max_iter, algorithm=None)
and analytical_sine(max_iter, algorithm=None)
for which I'm trying to compare their run time against the number of iterations computed. i.e. compare all 4 methods; 2 looped, 2 vectorised. Essentially my code goes something like this:
def analytical_sine(max_iter, algorithm=None):
if algorithm is None:
count = 0
analytical_hist = []
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
uni_dist = np.random.uniform(0, 1)
arccos = np.arccos(1 - 2*uni_dist)
analytical_hist.append(arccos)
elif algorithm is "vectorise":
analytical_hist = np.arccos(1 - 2*np.random.uniform(0, 1, max_iter))
return analytical_hist
def acc_rej_sine(max_iter, algorithm=None):
x = np.random.uniform(0, np.pi, max_iter)
y = np.random.rand(max_iter)
if algorithm is None:
accepted_x = []
j = count = 0
for i in range(max_iter):
count += 1
progress = round((count/max_iter)*100)
sys.stdout.write('r' + str(progress) + '%')
if y[i] <= np.sin(x[i]):
accepted_x.append(x[i])
j += 1
elif algorithm is "vectorise":
accepted_x = np.extract((y <= np.sin(x)), x)
return accepted_x
def runtime(func, runs, max_iter, algorithm=None):
time = []
for i in range(runs):
start = timer()
func()
end = timer()
time.append((end-start))
error = np.std(time)
time = sum(time)/runs
return time, error
def time_analysis():
time1, time2, time3, time4 = [], [], [], []
error1, error2, error3, error4 = [], [], [], []
for i in np.arange(1, 8, 1):
max_iter = 10**i
time, error = runtime(analytical_sine, 5, int(max_iter))
time1.append(time)
error1.append(error)
time, error = runtime(analytical_sine, 5, int(max_iter), "vectorise")
time2.append(time)
error2.append(error)
time, error = runtime(acc_rej_sine, 5, int(max_iter))
time3.append(time)
error3.append(error)
time, error = runtime(acc_rej_sine(max_iter), 5, int(max_iter), "vectorise")
time4.append(time)
error4.append(error)
return [time1, time2, time3, time4], [error1, error2, error3, error4]
# to run the code I would probably do something like this
time, error = time_analysis()
#then if I wanna plot number of iterations vs run time with errors I would probably do something along the lines of
plt.plot(max_iter, time) # max_iter would be a list of [10**i for i in np.arange(1, 8, 1)]
plt.errorbar(error)
So the idea is my runtime()
function would allow me to put in any of the 4 functions that I want to compare(which currently still isn't working yet and I can't/haven't figured out why), and run it for 5 times, work out the mean run time and standard deviation as the error. In return my time_analysis()
function would run runtime()
for different functions for different max_iter
(max iterations) which goes like [10, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7]
so I can plot max iterations against run time. However, this whole method seems quite cumbersome and inelegant, as my time_analysis()
requires me to repeatedly work out time and error and append it to a list. Is there a better way of timing this?(Also my runtime()
doesn't work yet lol because my algorithm
argument seems to be making something not callable
)
python data-analysis
python data-analysis
asked Apr 4 at 13:02
user3613025user3613025
235
235
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48608%2fis-there-a-better-way-to-do-run-time-analysis-than-this%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f48608%2fis-there-a-better-way-to-do-run-time-analysis-than-this%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown