Execution Time Difference between two bit-wise differential execution

25 Views Asked by At

I found an interesting phenomenon, when I try to do symmetric encryption (Bit-wise XOR execution) for a figure with another random figure. The execution time is different between encryption and decryption, and the difference might be 4 to 6 times (encryption period: 1.2 sec, decryption period: 0.2 sec). The encryption execution cost more time than decryption. I'm so confuse why causes the difference. I have attached the code below.

# Encryption Execution
start_time = time.time()
encrypted_figure = differential_fig(origin_img, input_random_img, 1)
end_time = time.time()
# Decryption Execution
start_time = time.time()
decrypted_figure = differential_fig(encrypted_figure, input_random_img, 0)
end_time = time.time()
# Called Function 
def element_xor(first_array, second_array):
    array_size = first_array.size
    output_array = np.zeros(array_size, dtype=np.int32)
    for e in range(array_size):
        output_array[e] = first_array[e] ^ second_array[e]
    return output_array

def sliding_window_differential(raw_array, random_array, raw_size, random_size, flag):
    indexes_list = []
    times = range(math.floor(raw_size / random_size))
    num_remain_elements = raw_size % random_size
    for j in times:
        start_index = j * random_size + num_remain_elements
        end_index = start_index + random_size
        indexes_list.append([start_index, end_index])
    iterative_index = [[0, random_size]] + indexes_list
    if not flag: # flag 1: encrypt, 0: decrypt
        iterative_index.reverse()
    for start_index, end_index in iterative_index:
        raw_array[start_index:end_index] = element_xor(raw_array[start_index:end_index],
                                                       random_array)
    return raw_array

def differential_fig(raw_img, random_img, encrypt_flag):
    o_h, o_w, o_c = raw_img.shape
    mid_img = random_img[0, :, :, :]
    output_figure = np.zeros((o_h, o_w, o_c), dtype=np.int32)
    for i in range(o_c):
        e_raw_img = raw_img[:, :, i].flatten()
        e_random_img = mid_img[:, :, i].flatten()
        origin_size = e_raw_img.size
        random_size = e_random_img.size
        if origin_size > random_size:
            e_raw_img = sliding_window_differential(e_raw_img, e_random_img, origin_size, 
                                                    random_size, encrypt_flag)
        else:
            e_random_img = e_random_img[:origin_size]
            e_raw_img = element_xor(e_raw_img, e_random_img)
        output_figure[:, :, i] = e_raw_img.reshape(o_h, o_w)
    return output_figure

Could anyone explain the phenomenon? Or give suggestion to improve the process.

1

There are 1 best solutions below

0
blhsing On

The list.reverse method creates a copy of the given list with the items in reverse order, and copying takes time proportional to the number items in the list. You can use the reverse function instead to generate the items in reverse order to avoid making a copy. There will still be overhead from calling the generator, but it will be much smaller than that of copying.

Change:

iterative_index.reverse()

to:

iterative_index = reverse(iteratie_index)