In this paper the smallest or optimal dimensions of a Halbach cylinder of a finite length for a given sample volume and desired flux density are determined using numerical modeling and parameter variation. A sample volume that is centered in and shaped as the Halbach cylinder bore but with a possible shorter length is considered. The external radius and the length of the Halbach cylinder with the smallest possible dimensions are found as a function of a desired internal radius, length of the sample volume and mean flux density. It is shown that the optimal ratio between the outer and inner radius of the Halbach cylinder does not depend on the length of the sample volume. Finally, the efficiency of a finite length Halbach cylinder is considered and compared with the case of a cylinder of infinite length. The most efficient dimensions for a Halbach cylinder are found and it is shown that the efficiency increases slowly with the length of the cylinder.