We study the problem of learning a Hamiltonian H to precision ε, supposing we are given copies of its Gibbs state ρ = exp(−βH)/ Tr(exp(−βH)) at a known inverse temperature β. Anshu, Arunachalam, Kuwahara, and Soleimanifar [AAKS21] recently studied the sample complexity (number of copies of ρ needed) of this problem for geometrically local N -qubit Hamiltonians. In the high-temperature (low β) regime, their algorithm has sample complexity poly(N, 1/β, 1/ε) and can be implemented with polynomial, but suboptimal, time complexity.In this paper, we study the same question for a more general class of Hamiltonians. We show how to learn the coefficients of a Hamiltonian to error ε with sample complexity S = O(log N/(βε) 2 ) and time complexity linear in the sample size, O(SN ). Furthermore, we prove a matching lower bound showing that our algorithm's sample complexity is optimal, and hence our time complexity is also optimal.In the appendix, we show that virtually the same algorithm can be used to learn H from a real-time evolution unitary e −itH in a small t regime with similar sample and time complexity.