Cloud computing provides the scalable computation capability based on a virtualization technique. The energy conservation for green computing is one of the vital issues while allocating resources. To improve energy efficiency, the dynamic power-saving resource allocation (DPRA) mechanism based on a particle swarm optimization algorithm is proposed. The DPRA mechanism not only considers the energy consumption of physical machine (PM) and virtual machine (VM) but also newly tackles the energy efficiency ratio of air conditioner. Moreover, the least squares regression method is utilized to forecast PM's resource utilization for allocating VM and eliminating VM migrations. In simulation, the proposed DPRA mechanism is compared with three familiar allocation schemes and one previous solution. Simulation results show that in the presence of VM number, DPRA outperforms traditional schemes and previous solution in terms of total energy consumption (includes PMs and air conditioners), total electric bill, VM migration, and the number of shutdown PMs, chosen as objective performance metrics.