Question: Efficient Code: I need to turn the code below into more efficient code while running with 2 cores, currently the code is not as efficient

Efficient Code:

I need to turn the code below into more efficient code while running with 2 cores, currently the code is not as efficient as it should be. Please help me figure this out.

Code:

import random import multiprocessing import math import time def picking_fresh_deck(): numbering_deck_of_cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,] numbering_deck_of_cards = numbering_deck_of_cards * number_of_decks_used random.shuffle(numbering_deck_of_cards) return numbering_deck_of_cards[:] def blackjack_simulation(starting_queue, runner_batch): complete_deck_of_cards = [] def blackjack_play_on(): storing_cards_dealer = [] storing_cards_player = [] storing_cards_player.append(complete_deck_of_cards.pop(0)) storing_cards_dealer.append(complete_deck_of_cards.pop(0)) storing_cards_player.append(complete_deck_of_cards.pop(0)) storing_cards_dealer.append(complete_deck_of_cards.pop(0)) while sum(storing_cards_player) < 12: storing_cards_player.append(complete_deck_of_cards.pop(0)) while sum(storing_cards_dealer) < 18: exit = False if sum(storing_cards_dealer) == 17: exit = True for i, card in enumerate(storing_cards_dealer): if card == 11: exit = False storing_cards_dealer[i] = 1 if exit: break storing_cards_dealer.append(complete_deck_of_cards.pop(0)) if sum(storing_cards_player) < sum(storing_cards_dealer) or sum(storing_cards_player) > 21: return 1; if sum(storing_cards_player) == sum(storing_cards_dealer): return 0; if sum(storing_cards_player) > sum(storing_cards_dealer): return -1; complete_deck_of_cards = picking_fresh_deck() declared_win = 0 declared_draw = 0 declared_lose = 0 for i in range(0, runner_batch): if (float(len(complete_deck_of_cards)) / (52 * number_of_decks_used)) * 100 < 75: complete_deck_of_cards = picking_fresh_deck() checking_final_output = blackjack_play_on() if checking_final_output == 1: declared_win += 1 if checking_final_output == 0: declared_draw += 1 if checking_final_output == -1: declared_lose += 1 starting_queue.put([declared_win, declared_draw, declared_lose]) def blackjack_runner(): list_of_declarations = [] declared_win = 0 declared_draw = 0 declared_lose = 0 runner_starts = time.time() counting_cpus_used = multiprocessing.cpu_count() runner_batch = int(math.ceil(no_of_simulations_to_run / float(counting_cpus_used))) starting_queue = multiprocessing.Queue() process_holder = [] for i in range(0, counting_cpus_used): single_process = multiprocessing.Process(target=blackjack_simulation, args=(starting_queue, runner_batch)) process_holder.append(single_process) single_process.start() for p in process_holder: p.join() runner_ends = time.time() - runner_starts for i in range(0, counting_cpus_used): checking_final_output = starting_queue.get() declared_win += checking_final_output[0] declared_draw += checking_final_output[1] declared_lose += checking_final_output[2] list_of_declarations = [declared_win, declared_draw, declared_lose] return list_of_declarations if __name__ == "__main__": no_of_simulations_to_run = 5000 print('Total number of Simulations ran: %d' % no_of_simulations_to_run) number_of_decks_used = 6 output = blackjack_runner() print('Count of total wins: %d' % output[0]) print('Count of total draws: %d' % output[1]) print('Count of total losses: %d' % output[2])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!