performance-aware-programming/haversine_01/python/haversine_json.py

41 lines
885 B
Python

# Naive Python version of the code
import os
from haversine_algorithm import haversine_of_degrees
import time
import json
filepath = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"data_10000000_flex.json",
)
with open(filepath, "r") as json_file:
start_time = time.time()
json_input = json.load(json_file)
mid_time = time.time()
earth_radius_km = 6371
sum = 0
count = 0
for pair in json_input["pairs"]:
sum += haversine_of_degrees(
pair["x0"], pair["y0"], pair["x1"], pair["y1"], earth_radius_km
)
count += 1
average = sum / count
end_time = time.time()
print(f"Result: {average}")
print(f"Input = {mid_time - start_time} seconds")
print(f"Math = {end_time - mid_time} seconds")
print(f"Total = {end_time - start_time} seconds")
print(f"Throughput = {count / (end_time - start_time)} haversines/second")