You have unsaved changes
Python
# !/usr/bin/python3
# -*- coding: utf-8 -*-

'''
Dictionary flip profiling
'''

import cProfile, pstats
from datetime import datetime
from faker import Faker

fake = Faker()
res = assoc_mock_array = {}

# ===================

Faker.seed(0)
assoc_mock_array = fake.pydict(nb_elements = 900, variable_nb_elements = False, value_types = ['int'])

i = 1
while i < 22:
    for k, v in assoc_mock_array.copy().items():
        new_key = (k[:6] + k[:2] + str(i)) if len(k + k) > 8 else k + k
        assoc_mock_array[new_key] = 999999-v+i
    i += 1

def custom_timer():
    return int(datetime.now().timestamp()*1000000)

print( 'Initial len of the dictionary: ', len(assoc_mock_array) )

# ===================

def swap_loop(mock_dict):
    return len( dict((v,k) for k,v in mock_dict.items()) )

def swap_map(mock_dict):
    return len( dict(map(reversed, mock_dict.items())) )

def swap_map_lambda(mock_dict):
    return len( dict(map(lambda x: x[::-1], mock_dict.items())) )

def swap_slice(mock_dict):
    return len( {mock_dict[i]:i for i in mock_dict} )

def swap_zip(mock_dict):
    return len( dict(zip(mock_dict.values(), mock_dict.keys())) )

# ===================

pr = cProfile.Profile(custom_timer, 0.000001, subcalls=False, builtins=False)

pr.enable()
print("\tReduced to: ", swap_loop(assoc_mock_array.copy()))
print("\tReduced to: ", swap_map(assoc_mock_array.copy()))
print("\tReduced to: ", swap_map_lambda(assoc_mock_array.copy()))
print("\tReduced to: ", swap_slice(assoc_mock_array.copy()))
print("\tReduced to: ", swap_zip(assoc_mock_array.copy()))
pr.disable()

pstats.Stats(pr).sort_stats('name').print_stats('swap_')

print('End!')
Initial len of the dictionary:  22590
	Reduced to:  12912
	Reduced to:  12912
	Reduced to:  12912
	Reduced to:  12912
	Reduced to:  12912
         45186 function calls in 0.530 seconds

   Ordered by: function name
   List reduced from 7 to 5 due to restriction <'swap_'>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.128    0.128    0.247    0.247 script.py:34(swap_loop)
        1    0.019    0.019    0.019    0.019 script.py:37(swap_map)
        1    0.130    0.130    0.256    0.256 script.py:40(swap_map_lambda)
        1    0.005    0.005    0.005    0.005 script.py:43(swap_slice)
        1    0.003    0.003    0.003    0.003 script.py:46(swap_zip)


End!