Files
core/test/performance/settings_reference.py
2026-11-27 06:03:56 +00:00

85 lines
2.8 KiB
Python

"""启动读取同一配置,热路径只借用文本并查询动态数字;对端不共享 Dever 运行库。"""
def config():
return {'module': {'main': {'main': {'corpus': '雪0123456789' * 64}}}}
def expected(n):
corpus = config()['module']['main']['main']['corpus']
total = 0
for residue in range(min(n, 1000)):
if str(residue) in corpus:
count = (n - 1 - residue) // 1000 + 1
total += residue * count + 1000 * count * (count - 1) // 2
return total
def source(language):
if language == 'dever':
return '''setting corpus: Text
rule selected(found: Bool, value: Int) (out: Int)
| found | value | out |
| true | - | value |
| false | - | 0 |
loop measure(n: Int) (out: Int)
| each value in range(0, n) | result |
| out | sum selected(text.contains(corpus, int.toText(value % 1000)), value) |
cmd bench = app.measure
'''
if language == 'rust':
return '''use serde::Deserialize;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Root { module: Module }
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Module { main: Domain }
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Domain { main: Settings }
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Settings { corpus: String }
fn measure(settings: &Settings, n: i64) -> i64 {
let mut total = 0i64;
for value in 0..n {
let text = (value % 1000).to_string();
let selected = if settings.corpus.contains(&text) { value } else { 0 };
total = total.checked_add(selected).unwrap();
}
total
}
fn main() {
let path = std::env::args().nth(2).unwrap();
let input = std::fs::read_to_string(path).unwrap();
let root: Root = serde_json::from_str(&input).unwrap();
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
println!("{}", measure(&root.module.main.main, n));
}
'''
return '''package main
import ("encoding/json"; "fmt"; "os"; "strconv"; "strings")
type Settings struct { Corpus string }
type Domain struct { Main Settings }
type Module struct { Main Domain }
type Root struct { Module Module }
func main() {
input, err := os.ReadFile(os.Args[2]); if err != nil { panic(err) }
var root Root
decoder := json.NewDecoder(strings.NewReader(string(input)))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&root); err != nil { panic(err) }
n, err := strconv.ParseInt(os.Args[1], 10, 64); if err != nil { panic(err) }
total := int64(0)
for value := int64(0); value < n; value++ {
text := strconv.FormatInt(value % 1000, 10)
selected := int64(0)
if strings.Contains(root.Module.Main.Main.Corpus, text) { selected = value }
next := total + selected
if next < total { panic("integer overflow") }; total = next
}
fmt.Println(total)
}
'''