File: no-gil-scaling.py

package info (click to toggle)
quantlib-swig 1.40-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,276 kB
  • sloc: python: 6,024; java: 1,552; cs: 774; makefile: 309; sh: 22
file content (41 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (2)
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
import sysconfig
import concurrent.futures
import time

import QuantLib as ql

if __name__ == "__main__":
    def pricing() -> float:
        todaysDate = ql.Date(15, ql.May, 2025)
        ql.Settings.instance().evaluationDate = todaysDate
        underlying = ql.SimpleQuote(7.0)
        dividendYield = ql.FlatForward(todaysDate, 0.05, ql.Actual365Fixed())
        volatility = ql.BlackConstantVol(todaysDate, ql.TARGET(), 0.10, ql.Actual365Fixed())
        riskFreeRate = ql.FlatForward(todaysDate, 0.05, ql.Actual365Fixed())

        process = ql.BlackScholesMertonProcess(
            ql.QuoteHandle(underlying),
            ql.YieldTermStructureHandle(dividendYield),
            ql.YieldTermStructureHandle(riskFreeRate),
            ql.BlackVolTermStructureHandle(volatility),
        )
        option = ql.VanillaOption(
            ql.PlainVanillaPayoff(ql.Option.Call, 8.0),
            ql.EuropeanExercise(ql.Date(17, ql.May, 2026))
        )
        engine = ql.FdBlackScholesVanillaEngine(process, 100, 8000)
        option.setPricingEngine(engine)
        return option.NPV()

    print("GIL is disabled: ", bool(sysconfig.get_config_var("Py_GIL_DISABLED")))

    for w in [1, 2, 4, 8, 16, 32, 64]:
        start = time.time()

        with concurrent.futures.ThreadPoolExecutor(max_workers=w) as executor:
            retVals = [executor.submit(pricing) for i in range(400)]

            for val in retVals:
                val.result()

        print(f"{w:2} threads, {400/(time.time()-start):5.1f} NPVs per sec")