Zig Version
0.17.0-dev.940+10083cc23
Steps to Reproduce, Observed Behavior, and Expected Behavior
Hi, I tried this with Zig after #35774, #35715
cat > switch_range_order_repro.py <<'PY'
#!/usr/bin/env python3
import os
import shutil
import subprocess
import time
from pathlib import Path
N = int(os.environ.get("N", "64000"))
ZIG = os.environ.get("ZIG", "zig")
ZIG_LIB = os.environ.get("ZIG_LIB")
def write_switch(path: Path, descending: bool) -> None:
values = range(N - 1, -1, -1) if descending else range(N)
with path.open("w", encoding="ascii") as f:
f.write("export fn f(x: u32) u32 {\n")
f.write(" return switch (x) {\n")
for value in values:
f.write(f" {value} => {value},\n")
f.write(" else => 0,\n")
f.write(" };\n")
f.write("}\n")
def run(name: str, descending: bool) -> None:
src = Path(f"{name}_{N}.zig")
cache = Path(f"cache_{name}")
if cache.exists():
shutil.rmtree(cache)
cache.mkdir()
Path("global-cache").mkdir(exist_ok=True)
write_switch(src, descending)
cmd = [
ZIG,
"build-obj",
str(src),
"-fno-emit-bin",
"--cache-dir", str(cache),
"--global-cache-dir", "global-cache",
]
if ZIG_LIB:
cmd[3:3] = ["--zig-lib-dir", ZIG_LIB]
start = time.perf_counter()
subprocess.run(cmd, check=True)
seconds = time.perf_counter() - start
print(f"{name}: {seconds:.3f}s source={src.stat().st_size}")
run("asc", False)
run("desc", True)
PY
ZIG=/path/to/zig python3 switch_range_order_repro.py
asc: 0.137s source=1513858
desc: 1.715s source=1513858
The descending/ascending ratio grows with input size:
N=8000 1.3x
N=16000 1.9x
N=32000 4.8x
N=64000 12.5x
### Zig Version
0.17.0-dev.940+10083cc23
### Steps to Reproduce, Observed Behavior, and Expected Behavior
Hi, I tried this with Zig after #35774, #35715
```shell
cat > switch_range_order_repro.py <<'PY'
#!/usr/bin/env python3
import os
import shutil
import subprocess
import time
from pathlib import Path
N = int(os.environ.get("N", "64000"))
ZIG = os.environ.get("ZIG", "zig")
ZIG_LIB = os.environ.get("ZIG_LIB")
def write_switch(path: Path, descending: bool) -> None:
values = range(N - 1, -1, -1) if descending else range(N)
with path.open("w", encoding="ascii") as f:
f.write("export fn f(x: u32) u32 {\n")
f.write(" return switch (x) {\n")
for value in values:
f.write(f" {value} => {value},\n")
f.write(" else => 0,\n")
f.write(" };\n")
f.write("}\n")
def run(name: str, descending: bool) -> None:
src = Path(f"{name}_{N}.zig")
cache = Path(f"cache_{name}")
if cache.exists():
shutil.rmtree(cache)
cache.mkdir()
Path("global-cache").mkdir(exist_ok=True)
write_switch(src, descending)
cmd = [
ZIG,
"build-obj",
str(src),
"-fno-emit-bin",
"--cache-dir", str(cache),
"--global-cache-dir", "global-cache",
]
if ZIG_LIB:
cmd[3:3] = ["--zig-lib-dir", ZIG_LIB]
start = time.perf_counter()
subprocess.run(cmd, check=True)
seconds = time.perf_counter() - start
print(f"{name}: {seconds:.3f}s source={src.stat().st_size}")
run("asc", False)
run("desc", True)
PY
ZIG=/path/to/zig python3 switch_range_order_repro.py
```
```
asc: 0.137s source=1513858
desc: 1.715s source=1513858
```
The descending/ascending ratio grows with input size:
```
N=8000 1.3x
N=16000 1.9x
N=32000 4.8x
N=64000 12.5x
```
@justusk