Different queries result in big difference in performance
The use of the pure SQL statement in the second loop makes the database engine compile the whole select 'a'|| v_i::character varying into  v_md5; on each loop iteration before evaluating it whereas the variable assignment is parsed easily into low level instructions once.