This version runs faster for me:
    
    
    
    import times
    
    const full_string = """I think this very thing is not like the thing that I 
think it is
    I would be happy to eat a banana collected from an apple tree, though I am 
not really sure
    it would taste like a banana - rather like an apple. What's the point in 
eating a banana that
    tastes like an apple? Or, for that matter, eating an apple that tastes like 
a banana?
    
    Some day I will explain you everything in detail. Today, gotta go since I 
have some performance
    comparisons to do. Bye!
    """
    
    const sub_string = "gotta go since"
    const N = 100000000
    
    proc strstr(haystack, needle: cstring): cstring {.importc, header: 
"<string.h>".}
    
    proc contains(haystack, needle: string): bool = strstr(cstring(haystack), 
cstring(needle)) != nil
    
    proc find_string_in_nim(fullstring: string, substring: string): bool =
        result = sub_string in full_string
    
    proc repeat(full_string: string, substring: string, n_iter: int): void =
        let time = cpuTime()
        for i in 0..n_iter:
            echo find_string_in_nim(full_string, substring)
        echo "Time taken: ", cpuTime() - time
    
    repeat(full_string, sub_string, N)
    
    
    Run

Reply via email to