forked from realFlowControl/1brc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMeasurements.php
483 lines (470 loc) · 20.4 KB
/
createMeasurements.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
class WeatherStation {
private $id;
private $meanTemperature;
public function __construct($id, $meanTemperature) {
$this->id = $id;
$this->meanTemperature = $meanTemperature;
}
public function measurement() {
$m = $this->gauss_ms($this->meanTemperature, 10);
return round($m * 10) / 10.0;
}
// PHP lacks a native Gaussian/normal distribution function, so we implement the Box-Muller transform
private function gauss_ms($mean, $stdDev) {
$u1 = mt_rand() / mt_getrandmax();
$u2 = mt_rand() / mt_getrandmax();
$randStdNormal = sqrt(-2 * log($u1)) * sin(2 * pi() * $u2);
return $mean + $stdDev * $randStdNormal;
}
public function getId() {
return $this->id;
}
}
function createMeasurements($numRecords) {
$measurementFile = './measurements.txt';
$start = microtime(true);
// Hardcoded data
$stations = [
new WeatherStation("Abha", 18.0),
new WeatherStation("Abidjan", 26.0),
new WeatherStation("Abéché", 29.4),
new WeatherStation("Accra", 26.4),
new WeatherStation("Addis Ababa", 16.0),
new WeatherStation("Adelaide", 17.3),
new WeatherStation("Aden", 29.1),
new WeatherStation("Ahvaz", 25.4),
new WeatherStation("Albuquerque", 14.0),
new WeatherStation("Alexandra", 11.0),
new WeatherStation("Alexandria", 20.0),
new WeatherStation("Algiers", 18.2),
new WeatherStation("Alice Springs", 21.0),
new WeatherStation("Almaty", 10.0),
new WeatherStation("Amsterdam", 10.2),
new WeatherStation("Anadyr", -6.9),
new WeatherStation("Anchorage", 2.8),
new WeatherStation("Andorra la Vella", 9.8),
new WeatherStation("Ankara", 12.0),
new WeatherStation("Antananarivo", 17.9),
new WeatherStation("Antsiranana", 25.2),
new WeatherStation("Arkhangelsk", 1.3),
new WeatherStation("Ashgabat", 17.1),
new WeatherStation("Asmara", 15.6),
new WeatherStation("Assab", 30.5),
new WeatherStation("Astana", 3.5),
new WeatherStation("Athens", 19.2),
new WeatherStation("Atlanta", 17.0),
new WeatherStation("Auckland", 15.2),
new WeatherStation("Austin", 20.7),
new WeatherStation("Baghdad", 22.77),
new WeatherStation("Baguio", 19.5),
new WeatherStation("Baku", 15.1),
new WeatherStation("Baltimore", 13.1),
new WeatherStation("Bamako", 27.8),
new WeatherStation("Bangkok", 28.6),
new WeatherStation("Bangui", 26.0),
new WeatherStation("Banjul", 26.0),
new WeatherStation("Barcelona", 18.2),
new WeatherStation("Bata", 25.1),
new WeatherStation("Batumi", 14.0),
new WeatherStation("Beijing", 12.9),
new WeatherStation("Beirut", 20.9),
new WeatherStation("Belgrade", 12.5),
new WeatherStation("Belize City", 26.7),
new WeatherStation("Benghazi", 19.9),
new WeatherStation("Bergen", 7.7),
new WeatherStation("Berlin", 10.3),
new WeatherStation("Bilbao", 14.7),
new WeatherStation("Birao", 26.5),
new WeatherStation("Bishkek", 11.3),
new WeatherStation("Bissau", 27.0),
new WeatherStation("Blantyre", 22.2),
new WeatherStation("Bloemfontein", 15.6),
new WeatherStation("Boise", 11.4),
new WeatherStation("Bordeaux", 14.2),
new WeatherStation("Bosaso", 30.0),
new WeatherStation("Boston", 10.9),
new WeatherStation("Bouaké", 26.0),
new WeatherStation("Bratislava", 10.5),
new WeatherStation("Brazzaville", 25.0),
new WeatherStation("Bridgetown", 27.0),
new WeatherStation("Brisbane", 21.4),
new WeatherStation("Brussels", 10.5),
new WeatherStation("Bucharest", 10.8),
new WeatherStation("Budapest", 11.3),
new WeatherStation("Bujumbura", 23.8),
new WeatherStation("Bulawayo", 18.9),
new WeatherStation("Burnie", 13.1),
new WeatherStation("Busan", 15.0),
new WeatherStation("Cabo San Lucas", 23.9),
new WeatherStation("Cairns", 25.0),
new WeatherStation("Cairo", 21.4),
new WeatherStation("Calgary", 4.4),
new WeatherStation("Canberra", 13.1),
new WeatherStation("Cape Town", 16.2),
new WeatherStation("Changsha", 17.4),
new WeatherStation("Charlotte", 16.1),
new WeatherStation("Chiang Mai", 25.8),
new WeatherStation("Chicago", 9.8),
new WeatherStation("Chihuahua", 18.6),
new WeatherStation("Chișinău", 10.2),
new WeatherStation("Chittagong", 25.9),
new WeatherStation("Chongqing", 18.6),
new WeatherStation("Christchurch", 12.2),
new WeatherStation("City of San Marino", 11.8),
new WeatherStation("Colombo", 27.4),
new WeatherStation("Columbus", 11.7),
new WeatherStation("Conakry", 26.4),
new WeatherStation("Copenhagen", 9.1),
new WeatherStation("Cotonou", 27.2),
new WeatherStation("Cracow", 9.3),
new WeatherStation("Da Lat", 17.9),
new WeatherStation("Da Nang", 25.8),
new WeatherStation("Dakar", 24.0),
new WeatherStation("Dallas", 19.0),
new WeatherStation("Damascus", 17.0),
new WeatherStation("Dampier", 26.4),
new WeatherStation("Dar es Salaam", 25.8),
new WeatherStation("Darwin", 27.6),
new WeatherStation("Denpasar", 23.7),
new WeatherStation("Denver", 10.4),
new WeatherStation("Detroit", 10.0),
new WeatherStation("Dhaka", 25.9),
new WeatherStation("Dikson", -11.1),
new WeatherStation("Dili", 26.6),
new WeatherStation("Djibouti", 29.9),
new WeatherStation("Dodoma", 22.7),
new WeatherStation("Dolisie", 24.0),
new WeatherStation("Douala", 26.7),
new WeatherStation("Dubai", 26.9),
new WeatherStation("Dublin", 9.8),
new WeatherStation("Dunedin", 11.1),
new WeatherStation("Durban", 20.6),
new WeatherStation("Dushanbe", 14.7),
new WeatherStation("Edinburgh", 9.3),
new WeatherStation("Edmonton", 4.2),
new WeatherStation("El Paso", 18.1),
new WeatherStation("Entebbe", 21.0),
new WeatherStation("Erbil", 19.5),
new WeatherStation("Erzurum", 5.1),
new WeatherStation("Fairbanks", -2.3),
new WeatherStation("Fianarantsoa", 17.9),
new WeatherStation("Flores, Petén", 26.4),
new WeatherStation("Frankfurt", 10.6),
new WeatherStation("Fresno", 17.9),
new WeatherStation("Fukuoka", 17.0),
new WeatherStation("Gabès", 19.5),
new WeatherStation("Gaborone", 21.0),
new WeatherStation("Gagnoa", 26.0),
new WeatherStation("Gangtok", 15.2),
new WeatherStation("Garissa", 29.3),
new WeatherStation("Garoua", 28.3),
new WeatherStation("George Town", 27.9),
new WeatherStation("Ghanzi", 21.4),
new WeatherStation("Gjoa Haven", -14.4),
new WeatherStation("Guadalajara", 20.9),
new WeatherStation("Guangzhou", 22.4),
new WeatherStation("Guatemala City", 20.4),
new WeatherStation("Halifax", 7.5),
new WeatherStation("Hamburg", 9.7),
new WeatherStation("Hamilton", 13.8),
new WeatherStation("Hanga Roa", 20.5),
new WeatherStation("Hanoi", 23.6),
new WeatherStation("Harare", 18.4),
new WeatherStation("Harbin", 5.0),
new WeatherStation("Hargeisa", 21.7),
new WeatherStation("Hat Yai", 27.0),
new WeatherStation("Havana", 25.2),
new WeatherStation("Helsinki", 5.9),
new WeatherStation("Heraklion", 18.9),
new WeatherStation("Hiroshima", 16.3),
new WeatherStation("Ho Chi Minh City", 27.4),
new WeatherStation("Hobart", 12.7),
new WeatherStation("Hong Kong", 23.3),
new WeatherStation("Honiara", 26.5),
new WeatherStation("Honolulu", 25.4),
new WeatherStation("Houston", 20.8),
new WeatherStation("Ifrane", 11.4),
new WeatherStation("Indianapolis", 11.8),
new WeatherStation("Iqaluit", -9.3),
new WeatherStation("Irkutsk", 1.0),
new WeatherStation("Istanbul", 13.9),
new WeatherStation("İzmir", 17.9),
new WeatherStation("Jacksonville", 20.3),
new WeatherStation("Jakarta", 26.7),
new WeatherStation("Jayapura", 27.0),
new WeatherStation("Jerusalem", 18.3),
new WeatherStation("Johannesburg", 15.5),
new WeatherStation("Jos", 22.8),
new WeatherStation("Juba", 27.8),
new WeatherStation("Kabul", 12.1),
new WeatherStation("Kampala", 20.0),
new WeatherStation("Kandi", 27.7),
new WeatherStation("Kankan", 26.5),
new WeatherStation("Kano", 26.4),
new WeatherStation("Kansas City", 12.5),
new WeatherStation("Karachi", 26.0),
new WeatherStation("Karonga", 24.4),
new WeatherStation("Kathmandu", 18.3),
new WeatherStation("Khartoum", 29.9),
new WeatherStation("Kingston", 27.4),
new WeatherStation("Kinshasa", 25.3),
new WeatherStation("Kolkata", 26.7),
new WeatherStation("Kuala Lumpur", 27.3),
new WeatherStation("Kumasi", 26.0),
new WeatherStation("Kunming", 15.7),
new WeatherStation("Kuopio", 3.4),
new WeatherStation("Kuwait City", 25.7),
new WeatherStation("Kyiv", 8.4),
new WeatherStation("Kyoto", 15.8),
new WeatherStation("La Ceiba", 26.2),
new WeatherStation("La Paz", 23.7),
new WeatherStation("Lagos", 26.8),
new WeatherStation("Lahore", 24.3),
new WeatherStation("Lake Havasu City", 23.7),
new WeatherStation("Lake Tekapo", 8.7),
new WeatherStation("Las Palmas de Gran Canaria", 21.2),
new WeatherStation("Las Vegas", 20.3),
new WeatherStation("Launceston", 13.1),
new WeatherStation("Lhasa", 7.6),
new WeatherStation("Libreville", 25.9),
new WeatherStation("Lisbon", 17.5),
new WeatherStation("Livingstone", 21.8),
new WeatherStation("Ljubljana", 10.9),
new WeatherStation("Lodwar", 29.3),
new WeatherStation("Lomé", 26.9),
new WeatherStation("London", 11.3),
new WeatherStation("Los Angeles", 18.6),
new WeatherStation("Louisville", 13.9),
new WeatherStation("Luanda", 25.8),
new WeatherStation("Lubumbashi", 20.8),
new WeatherStation("Lusaka", 19.9),
new WeatherStation("Luxembourg City", 9.3),
new WeatherStation("Lviv", 7.8),
new WeatherStation("Lyon", 12.5),
new WeatherStation("Madrid", 15.0),
new WeatherStation("Mahajanga", 26.3),
new WeatherStation("Makassar", 26.7),
new WeatherStation("Makurdi", 26.0),
new WeatherStation("Malabo", 26.3),
new WeatherStation("Malé", 28.0),
new WeatherStation("Managua", 27.3),
new WeatherStation("Manama", 26.5),
new WeatherStation("Mandalay", 28.0),
new WeatherStation("Mango", 28.1),
new WeatherStation("Manila", 28.4),
new WeatherStation("Maputo", 22.8),
new WeatherStation("Marrakesh", 19.6),
new WeatherStation("Marseille", 15.8),
new WeatherStation("Maun", 22.4),
new WeatherStation("Medan", 26.5),
new WeatherStation("Mek'ele", 22.7),
new WeatherStation("Melbourne", 15.1),
new WeatherStation("Memphis", 17.2),
new WeatherStation("Mexicali", 23.1),
new WeatherStation("Mexico City", 17.5),
new WeatherStation("Miami", 24.9),
new WeatherStation("Milan", 13.0),
new WeatherStation("Milwaukee", 8.9),
new WeatherStation("Minneapolis", 7.8),
new WeatherStation("Minsk", 6.7),
new WeatherStation("Mogadishu", 27.1),
new WeatherStation("Mombasa", 26.3),
new WeatherStation("Monaco", 16.4),
new WeatherStation("Moncton", 6.1),
new WeatherStation("Monterrey", 22.3),
new WeatherStation("Montreal", 6.8),
new WeatherStation("Moscow", 5.8),
new WeatherStation("Mumbai", 27.1),
new WeatherStation("Murmansk", 0.6),
new WeatherStation("Muscat", 28.0),
new WeatherStation("Mzuzu", 17.7),
new WeatherStation("N'Djamena", 28.3),
new WeatherStation("Naha", 23.1),
new WeatherStation("Nairobi", 17.8),
new WeatherStation("Nakhon Ratchasima", 27.3),
new WeatherStation("Napier", 14.6),
new WeatherStation("Napoli", 15.9),
new WeatherStation("Nashville", 15.4),
new WeatherStation("Nassau", 24.6),
new WeatherStation("Ndola", 20.3),
new WeatherStation("New Delhi", 25.0),
new WeatherStation("New Orleans", 20.7),
new WeatherStation("New York City", 12.9),
new WeatherStation("Ngaoundéré", 22.0),
new WeatherStation("Niamey", 29.3),
new WeatherStation("Nicosia", 19.7),
new WeatherStation("Niigata", 13.9),
new WeatherStation("Nouadhibou", 21.3),
new WeatherStation("Nouakchott", 25.7),
new WeatherStation("Novosibirsk", 1.7),
new WeatherStation("Nuuk", -1.4),
new WeatherStation("Odesa", 10.7),
new WeatherStation("Odienné", 26.0),
new WeatherStation("Oklahoma City", 15.9),
new WeatherStation("Omaha", 10.6),
new WeatherStation("Oranjestad", 28.1),
new WeatherStation("Oslo", 5.7),
new WeatherStation("Ottawa", 6.6),
new WeatherStation("Ouagadougou", 28.3),
new WeatherStation("Ouahigouya", 28.6),
new WeatherStation("Ouarzazate", 18.9),
new WeatherStation("Oulu", 2.7),
new WeatherStation("Palembang", 27.3),
new WeatherStation("Palermo", 18.5),
new WeatherStation("Palm Springs", 24.5),
new WeatherStation("Palmerston North", 13.2),
new WeatherStation("Panama City", 28.0),
new WeatherStation("Parakou", 26.8),
new WeatherStation("Paris", 12.3),
new WeatherStation("Perth", 18.7),
new WeatherStation("Petropavlovsk-Kamchatsky", 1.9),
new WeatherStation("Philadelphia", 13.2),
new WeatherStation("Phnom Penh", 28.3),
new WeatherStation("Phoenix", 23.9),
new WeatherStation("Pittsburgh", 10.8),
new WeatherStation("Podgorica", 15.3),
new WeatherStation("Pointe-Noire", 26.1),
new WeatherStation("Pontianak", 27.7),
new WeatherStation("Port Moresby", 26.9),
new WeatherStation("Port Sudan", 28.4),
new WeatherStation("Port Vila", 24.3),
new WeatherStation("Port-Gentil", 26.0),
new WeatherStation("Portland (OR)", 12.4),
new WeatherStation("Porto", 15.7),
new WeatherStation("Prague", 8.4),
new WeatherStation("Praia", 24.4),
new WeatherStation("Pretoria", 18.2),
new WeatherStation("Pyongyang", 10.8),
new WeatherStation("Rabat", 17.2),
new WeatherStation("Rangpur", 24.4),
new WeatherStation("Reggane", 28.3),
new WeatherStation("Reykjavík", 4.3),
new WeatherStation("Riga", 6.2),
new WeatherStation("Riyadh", 26.0),
new WeatherStation("Rome", 15.2),
new WeatherStation("Roseau", 26.2),
new WeatherStation("Rostov-on-Don", 9.9),
new WeatherStation("Sacramento", 16.3),
new WeatherStation("Saint Petersburg", 5.8),
new WeatherStation("Saint-Pierre", 5.7),
new WeatherStation("Salt Lake City", 11.6),
new WeatherStation("San Antonio", 20.8),
new WeatherStation("San Diego", 17.8),
new WeatherStation("San Francisco", 14.6),
new WeatherStation("San Jose", 16.4),
new WeatherStation("San José", 22.6),
new WeatherStation("San Juan", 27.2),
new WeatherStation("San Salvador", 23.1),
new WeatherStation("Sana'a", 20.0),
new WeatherStation("Santo Domingo", 25.9),
new WeatherStation("Sapporo", 8.9),
new WeatherStation("Sarajevo", 10.1),
new WeatherStation("Saskatoon", 3.3),
new WeatherStation("Seattle", 11.3),
new WeatherStation("Ségou", 28.0),
new WeatherStation("Seoul", 12.5),
new WeatherStation("Seville", 19.2),
new WeatherStation("Shanghai", 16.7),
new WeatherStation("Singapore", 27.0),
new WeatherStation("Skopje", 12.4),
new WeatherStation("Sochi", 14.2),
new WeatherStation("Sofia", 10.6),
new WeatherStation("Sokoto", 28.0),
new WeatherStation("Split", 16.1),
new WeatherStation("St. John's", 5.0),
new WeatherStation("St. Louis", 13.9),
new WeatherStation("Stockholm", 6.6),
new WeatherStation("Surabaya", 27.1),
new WeatherStation("Suva", 25.6),
new WeatherStation("Suwałki", 7.2),
new WeatherStation("Sydney", 17.7),
new WeatherStation("Tabora", 23.0),
new WeatherStation("Tabriz", 12.6),
new WeatherStation("Taipei", 23.0),
new WeatherStation("Tallinn", 6.4),
new WeatherStation("Tamale", 27.9),
new WeatherStation("Tamanrasset", 21.7),
new WeatherStation("Tampa", 22.9),
new WeatherStation("Tashkent", 14.8),
new WeatherStation("Tauranga", 14.8),
new WeatherStation("Tbilisi", 12.9),
new WeatherStation("Tegucigalpa", 21.7),
new WeatherStation("Tehran", 17.0),
new WeatherStation("Tel Aviv", 20.0),
new WeatherStation("Thessaloniki", 16.0),
new WeatherStation("Thiès", 24.0),
new WeatherStation("Tijuana", 17.8),
new WeatherStation("Timbuktu", 28.0),
new WeatherStation("Tirana", 15.2),
new WeatherStation("Toamasina", 23.4),
new WeatherStation("Tokyo", 15.4),
new WeatherStation("Toliara", 24.1),
new WeatherStation("Toluca", 12.4),
new WeatherStation("Toronto", 9.4),
new WeatherStation("Tripoli", 20.0),
new WeatherStation("Tromsø", 2.9),
new WeatherStation("Tucson", 20.9),
new WeatherStation("Tunis", 18.4),
new WeatherStation("Ulaanbaatar", -0.4),
new WeatherStation("Upington", 20.4),
new WeatherStation("Ürümqi", 7.4),
new WeatherStation("Vaduz", 10.1),
new WeatherStation("Valencia", 18.3),
new WeatherStation("Valletta", 18.8),
new WeatherStation("Vancouver", 10.4),
new WeatherStation("Veracruz", 25.4),
new WeatherStation("Vienna", 10.4),
new WeatherStation("Vientiane", 25.9),
new WeatherStation("Villahermosa", 27.1),
new WeatherStation("Vilnius", 6.0),
new WeatherStation("Virginia Beach", 15.8),
new WeatherStation("Vladivostok", 4.9),
new WeatherStation("Warsaw", 8.5),
new WeatherStation("Washington, D.C.", 14.6),
new WeatherStation("Wau", 27.8),
new WeatherStation("Wellington", 12.9),
new WeatherStation("Whitehorse", -0.1),
new WeatherStation("Wichita", 13.9),
new WeatherStation("Willemstad", 28.0),
new WeatherStation("Winnipeg", 3.0),
new WeatherStation("Wrocław", 9.6),
new WeatherStation("Xi'an", 14.1),
new WeatherStation("Yakutsk", -8.8),
new WeatherStation("Yangon", 27.5),
new WeatherStation("Yaoundé", 23.8),
new WeatherStation("Yellowknife", -4.3),
new WeatherStation("Yerevan", 12.4),
new WeatherStation("Yinchuan", 9.0),
new WeatherStation("Zagreb", 10.7),
new WeatherStation("Zanzibar City", 26.0),
new WeatherStation("Zürich", 9.3),
];
$fileHandle = fopen($measurementFile, 'w');
if (!$fileHandle) {
throw new Exception("Cannot open file: " . $measurementFile);
}
for ($i = 0; $i < $numRecords; $i++) {
if ($i > 0 && $i % 50000 === 0) {
$timeElapsed = microtime(true) - $start;
echo "Wrote $i measurements in $timeElapsed ms\n";
}
$station = $stations[array_rand($stations)];
fwrite($fileHandle, $station->getId() . ";" . $station->measurement() . "\n");
}
fclose($fileHandle);
$totalTime = microtime(true) - $start;
echo "Created file with $numRecords measurements in $totalTime ms\n";
}
// Usage example, passing argument from command line
if ($argc !== 2) {
echo "Usage: create_measurements.php <number of records to create>\n";
exit(1);
}
try {
$size = (int) $argv[1];
createMeasurements($size);
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage() . "\n";
exit(1);
}