Skip to content

Instantly share code, notes, and snippets.

import UIKit
var records = [(String, Double, Double)]()
func compareOperations(name: String, repeats: Int, operations: [()->()]) {
var record = (name, 0.0, 0.0)
for (index, opertion) in operations.enumerated() {
let startTime = CFAbsoluteTimeGetCurrent()
for _ in 1...repeats {
opertion()
var comparisonRecords = [(String, Int, Double, Double)]()
let trials = 10
func compareOperations(name: String, repeats: Int, arrayOperation: ()->(), setOperation: ()->()) {
for trial in 1...trials {
let arrayStartTime = CFAbsoluteTimeGetCurrent()
for _ in 1...repeats {
arrayOperation()
}
let arrayTimeInterval = (CFAbsoluteTimeGetCurrent() - arrayStartTime)*1000
import pandas as pd
from scipy.stats import ttest_ind
with open('array_set.txt', 'r') as file:
data_text = file.read()
splitted = data_text.strip('[(').strip(')]').strip(')]\n').split('), (')
data_list = []
for record in splitted:
compareOperations(name: "Initialization Without Elements", repeats: 10000, arrayOperation: {
let _ : [Int] = []
}) {
let _ : Set<Int> = []
}
compareOperations(name: "Initialization With Elements", repeats: 10000, arrayOperation: {
let _ : [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
}) {
let _ : Set<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 6 columns, instead of 5. in line 9.
Number, Experiment, Array_Time, Set_Time, t_value, p_value
1, "Initialization Without Elements", 7.540121674537659, 7.916748523712158, -3.457562191664637, 0.003846372791488026
2,"Initialization With Elements", 8.958488702774048, 24.988874793052673, -49.77387985600688, 3.71485625191658e-17
3,"Iterate Elements", 337.66061067581177, 44.00688409805298, 21.476253166244224, 4.086700260362606e-12
4,"Count Elements", 8.78673791885376, 8.702144026756287, 0.5112195802639851, 0.6171643091164576
5,"Check If Empty", 9.673908352851868, 9.035870432853699, 3.3699231118055515, 0.004578823247822731
6,"Check If Element Exists (When True)", 44.153839349746704, 9.419366717338562, 17.398721378711933, 7.04543940804122e-11
7,"Check If Element Exists (When False)", 357.1058511734009, 9.227603673934937, 44.28782375256025, 1.8869990362188217e-16
8,"Find Element Index (When Found)", 38.447365164756775, 9.576007723808289, 38.59760754000621, 1.2750267004908994e-15
9,"Find Element Index (When nil)", 276.5122652053833, 9.49104130268097, 75.
Operation Performance (> means shorter time)
Initilization Array > Set
Iteration Set > Array
Count Set = Array
Check Empty Set = Array
Find Element Existence or Index Set > Array
Find Min & Max Set > Array
Sort Elements Set = Array
Add Element Set = Array
Remove Element(s) Set = Array