Moved the haversine code to a subdirectory
This commit is contained in:
		
							
								
								
									
										14
									
								
								haversine/python/haversine_algorithm.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								haversine/python/haversine_algorithm.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
from math import radians, sin, cos, sqrt, asin
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def haversine_of_degrees(x0, y0, x1, y1, radius):
 | 
			
		||||
    dy = radians(y1 - y0)
 | 
			
		||||
    dx = radians(x1 - x0)
 | 
			
		||||
    y0 = radians(y0)
 | 
			
		||||
    y1 = radians(y1)
 | 
			
		||||
 | 
			
		||||
    root_term = (sin(dy / 2) ** 2) + cos(y0) * cos(y1) * (sin(dx / 2) ** 2)
 | 
			
		||||
 | 
			
		||||
    result = 2 * radius * asin(sqrt(root_term))
 | 
			
		||||
 | 
			
		||||
    return result
 | 
			
		||||
							
								
								
									
										40
									
								
								haversine/python/haversine_json.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								haversine/python/haversine_json.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
# 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")
 | 
			
		||||
		Reference in New Issue
	
	Block a user