Short answer formatted to fit the question
script.sh | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' >> /var/log/logfile
Explanation
awk
runs fast and is able to work as Unix pipe filter and print date by itself.
gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
Let's benchmark it:
yes |head -5000000 |gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' |uniq -c
461592 [2017-02-28 19:46:44] y
488555 [2017-02-28 19:46:45] y
491205 [2017-02-28 19:46:46] y
498568 [2017-02-28 19:46:47] y
502605 [2017-02-28 19:46:48] y
494048 [2017-02-28 19:46:49] y
493299 [2017-02-28 19:46:50] y
498005 [2017-02-28 19:46:51] y
502916 [2017-02-28 19:46:52] y
495550 [2017-02-28 19:46:53] y
73657 [2017-02-28 19:46:54] y
Additional information
Sed appears to runs much faster,
sed -e "s/^/$(date -R) /"
yes |head -5000000 |sed -e "s/^/$(date -R) /" |uniq -c
5000000 Tue, 28 Feb 2017 19:57:00 -0500 y
However, on closer inspection, set does not seem to change time,
vmstat 1 | sed -e "s/^/$(date -R) /"
Because date
(which is slower by the way) gets called only once.