Strange behaviour in Speed of SHA256

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Hardcore96
Posts: 2
Joined: Tue Aug 18, 2020 2:26 pm

Strange behaviour in Speed of SHA256

Post by Hardcore96 » Tue Aug 18, 2020 2:29 pm

Hello,

when I run this program on ESP32:

Code: Select all

from uhashlib import sha256
import time

def profile_sha256():
    data1 = b'00000000'
    data2 = b'11111111'
    data3 = b'0000000000000000'
    data4 = b'0101010101010101010101010101010101010101010101010101010101010101'
    data5 = data4 + data4
    data6 = data5 + data5 + data5 + data5
    data6 = data6 + data6 + data6 + data6

    N = 10000

    for data in [data1, data2, data3, data4, data5, data6]:
        start = time.ticks_ms()
        for _ in range(N):
            X = sha256()
            X.update(data)
            # Y = X.digest()   ### line commented out

        end = time.ticks_ms()
        duration = end - start

        print("======== For data length %d bytes: " % len(data))
        print("    Running %d sha256 in %f seconds" % (N, duration/1000))
        print("    Meaning 1 SHA256 calculation takes %f ms" % (duration/N))
I get these results:

Code: Select all

======== For data length 8 bytes: 
    Running 10000 sha256 in 1.754000 seconds
    Meaning 1 SHA256 calculation takes 0.175400 ms
======== For data length 8 bytes: 
    Running 10000 sha256 in 1.773000 seconds
    Meaning 1 SHA256 calculation takes 0.177300 ms
======== For data length 16 bytes: 
    Running 10000 sha256 in 1.779000 seconds
    Meaning 1 SHA256 calculation takes 0.177900 ms
======== For data length 64 bytes: 
    Running 10000 sha256 in 2.150000 seconds
    Meaning 1 SHA256 calculation takes 0.215000 ms
======== For data length 128 bytes: 
    Running 10000 sha256 in 2.403000 seconds
    Meaning 1 SHA256 calculation takes 0.240300 ms
======== For data length 2048 bytes: 
    Running 10000 sha256 in 9.807000 seconds
    Meaning 1 SHA256 calculation takes 0.980700 ms
However, when I uncomment the line

Code: Select all

Y = X.digest()
, the runtime significantly decreases. Can somebody explain what is going on here?

Code: Select all

======== For data length 8 bytes: 
    Running 10000 sha256 in 0.768000 seconds
    Meaning 1 SHA256 calculation takes 0.076800 ms
======== For data length 8 bytes: 
    Running 10000 sha256 in 0.771000 seconds
    Meaning 1 SHA256 calculation takes 0.077100 ms
======== For data length 16 bytes: 
    Running 10000 sha256 in 0.771000 seconds
    Meaning 1 SHA256 calculation takes 0.077100 ms
======== For data length 64 bytes: 
    Running 10000 sha256 in 1.015000 seconds
    Meaning 1 SHA256 calculation takes 0.101500 ms
======== For data length 128 bytes: 
    Running 10000 sha256 in 1.261000 seconds
    Meaning 1 SHA256 calculation takes 0.126100 ms
======== For data length 2048 bytes: 
    Running 10000 sha256 in 8.659000 seconds
    Meaning 1 SHA256 calculation takes 0.865900 ms

Post Reply