In magnetic atomic gases, the dipolar relaxation process couples the system spin and kinetic degrees of freedom, facilitating the conversion of kinetic energy into Zeeman energy. The atoms that transfer to high spin states in the dipolar relaxation process can be repumped to the ground state by utilizing optical pumping, thereby achieving a continuous cooling cycle and effectively lowering the system temperature. Because the energy removed in a single cooling cycle is much larger than the energy of scattered photons, this demagnetization cooling scheme significantly improves cooling efficiency and reduces atomic loss. In this work, we establish state-coupled equations that integrate dipolar relaxation and optical pumping to analyze the demagnetization cooling process, and model the evolution of atom number and temperature during the cooling of $^{164}{\text{Dy}}$ atoms. We develop a strategy to generate an optimal magnetic field waveform by maximizing the demagnetization rate. According to this strategy, we investigate the influence of crucial experimental parameters on demagnetization cooling and determine their specific ranges for producing large atom number of BEC, including the optical dipole trap frequency, as well as the intensity and polarization purity of the optical pumping light. The results indicate that demagnetization cooling can not only achieve an efficiency of $\chi \approx 44.92$, which is an order of magnitude higher than that from the traditional evaporative cooling, but also directly prepare Bose-Einstein condensate (BEC) of dysprosium with a large atomic number on a sub-second timescale, reducing the cooling time by one order of magnitude compared with that from the traditional methods of cooling dysprosium atoms.