Energy optimization with time constraint has become a timely and significant challenge for the datacenters. In this paper, a hardware and software collaborative optimization strategy is implemented to minimize the energy cost while satisfying the time constraint of the datacenters. In the hardware aspect, a DVFS‐capable CPU/GPU/FPGA heterogeneous computing infrastructure is built. This infrastructure can adjust its hardware characteristics dynamically in terms of the software run‐time contexts so that the applications can be executed efficiently with less time and lower energy cost. In the software aspect, a deadline‐aware energy‐efficient task scheduling algorithm based on the Q‐learning approach is investigated. This algorithm can adjust its searching directions smartly in terms of the environment feedback so that it can achieve better optimization performance comparing with the traditional genetic algorithm. However, its convergence time is long due to the large amount of training work, making it inappropriate to be applied in the large‐scale datacenters. To ease this problem, we proposed another new algorithm named Rapid Local Convolution Optimization (RLCO) and combine it with the Q‐learning algorithm. By doing this, the convergence time of the Q‐learning mechanism can be decreased significantly. We conducted both the simulation and real‐world experiments to evaluate the performance of our approaches, and the results proved the proposed algorithm running on the DVFS‐capable heterogeneous hardware architecture could decrease the energy cost of the datacenter significantly even if the datacenter is in large scale.