Main Content


Time required to run function on GPU



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.


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.


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)

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)
t1 = gputimeit(f,1)

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.


  • The function F must not call tic or toc.

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


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