Main Content

gputimeit

Time required to run function on GPU

Description

t = gputimeit(F) measures the typical time, in seconds, required to run the function specified by the function handle F. The function handle accepts no external input arguments, but you can define it with input arguments to its internal function call.

example

t = gputimeit(F,numOutputs) calls F with the desired number of output arguments, numOutputs. By default, gputimeit calls the function F with one output argument, or no output arguments if F does not return any output.

example

Examples

collapse all

This example shows how to measure the time to calculate sum(A.' .* B, 1) on a GPU, where A is a 12000-by-400 matrix and B is 400-by-12000.

A = rand(12000,400,'gpuArray');
B = rand(400,12000,'gpuArray');
f = @() sum(A.' .* B, 1);
t = gputimeit(f)
0.0026

Compare the time to run svd on a GPU, with one versus three output arguments.

X = rand(1000,'gpuArray');
f = @() svd(X);
t3 = gputimeit(f,3)
1.0622
t1 = gputimeit(f,1)
0.2933

Input Arguments

collapse all

Function to measure, specified as a function handle.

Number of output arguments to use in the function call, specified as a scalar integer.

If the function specified by F has a variable number of outputs, numOutputs specifies which syntax gputimeit uses to call the function. For example, the svd function returns a single output, s, or three outputs, [U,S,V]. Set numOutputs to 1 to time the s = svd(X) syntax, or set it to 3 to time the [U,S,V] = svd(X) syntax.

Limitations

  • The function F must not call tic or toc.

  • You cannot use tic and toc to measure the execution time of gputimeit itself.

Tips

gputimeit is preferable to timeit for functions that use the GPU, because it ensures that all operations on the GPU have finished before recording the time and compensates for the overhead. For operations that do not use a GPU, timeit offers greater precision.

Extended Capabilities

Version History

Introduced in R2013b