Sujet : Re: Performance of list / array / dict compared De : neumann (at) *nospam* wu-wien.ac.at (gustafn) Groupes :comp.lang.tcl Date : 19. Aug 2024, 09:35:29 Autres entêtes Organisation : novaBBS Message-ID :<52b7365dfcec510a4600ab6b0daad03c@www.novabbs.com> References :1234 User-Agent : Rocksolid Light
Hi RodionGork, I took a quick look at the "primes" examples in your comparison on the GitHub page. Using any data structures other than lists does not make sense for this example. One could get an improvement of about 5% by putting the outer loop into a proc. Most of the time in this example is spent in the "is_prime" proc. One can get much bigger improvements by using critcl for the is_prime function (see below): baseline list 1766907.44 100.00 loop proc 1689220.00 95.60 is_prime_list_c 118298.50 6.70 This is in the spirit of thinking in "system languages" and "glue languages" by John Ousterhout, where one should find the right mix for the applications, when performance matters. all the best -g =================================================================================== package require critcl critcl::cproc is_prime_list_c {Tcl_Interp* interp list primes int x} int { int i; for (i=0; i<primes.c; i++) { int d; if (Tcl_GetIntFromObj(interp, primes.v[i], &d) != TCL_OK) { fprintf(stderr, "list element is not an integer: '%s'\n", Tcl_GetString(primes.v[i])); } if (d*d > x) return 1; if (x%d == 0) return 0; } return -1; } critcl::load =================================================================================== =================================================================================== proc run_list_c {} { set primes {2 3 5 7} set n $::env(MAXN) for {set i 9} {1} {incr i 2} { if {[is_prime_list_c $primes $i]} { lappend primes $i if {[llength $primes] == $n} { puts "primes\[$n\] = $i" break } } } } ===================================================================================