I wouldn't worry about it too much, since print requires IO, it is by far the bottleneck.
For the rest of the utilities that I used: range, chain, and str.center: they are all implemented in C if you are using the standard CPython (should be fast).
To avoid the IO, let's compare the two functions as generators of strings (I replaced the print with yield (('*' * i).center(n)) for both my implementation and Nicks:
In [39]: %timeit tuple(my_diamond(11))
3.59 µs ± 37.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [40]: %timeit tuple(nicks_diamond(11))
4.66 µs ± 69.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Seems like mine is slower than Nicks.
However, on my machine, a single print statement takes about 4.17us, which is almost as long as diamond(11) takes without any prints!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Is this fast too?
I wouldn't worry about it too much, since
printrequires IO, it is by far the bottleneck.For the rest of the utilities that I used:
range,chain, andstr.center: they are all implemented inCif you are using the standardCPython(should be fast).To avoid the IO, let's compare the two functions as generators of strings (I replaced the
printwithyield (('*' * i).center(n))for both my implementation and Nicks:Seems like mine is slower than Nicks.
However, on my machine, a single print statement takes about 4.17us, which is almost as long as
diamond(11)takes without any prints!