Hi there folks!

It’s been a while since I started to learn rust as a 2020 goal. For that I´m using the Exercism platform to study and practice.

I´ll be writing a series of articles with everything find interesting, I hope you all like.

Reverse String Problem

The reverse string problem request some very simple problem, get a string and reverse the positions of the characters. Soon I just developed a simple code:

It passed the cargo test and I submitted it right away. But I always like to see the most commented community solution after I wrote mine. And for my surprise this solution used graphemes:

There were people who didn’t even know graphemes, it’s the smallest unit of a writing system. Some languages makes it’s uses more explicit like Elixir Strings.

Looking some of the commentaries I decided to make a simple benchmark, thinking that the reason to use a grapheme crate were because of performance.

So I looked over some benchmark library and found Criterion. I like the syntax and added it to the project. My first run was slower the the grapheme one:

Here is the code:

It created a black-box and runned the codes for a number of times or for a period of time, from that it gives you some measures.

I worked so on my code until I get the following:

There I learned much more about Rust and how it can be simpler than I was making it. the collect function can retrieve a String from a Vec<char> and the code got a lot smaller and faster.

I had submitted the code once again happy with that gain in performance and got an email with the approvement of my new solution but there were a message:

What was this test_grapheme_clusters? I searched for it in the code and found:

In my previous tests it didn’t runned because I of this part: #[cfg(feature = “grapheme”)]. It’s a conditional compilation and to run this piece with all the others ones I had to use the command: cargo test — features “grapheme”. For my surprise the result:

As you can see when we remove the escape characters we have the following string: “u\{308}uu”, the tricky part is that u\u{308} is a single grapheme, but not a single character and it represents ü.

So with that I had to change my code once again:

Don’t mind the red underline, it´s working.

As you can see, now the code passed in all tests, but what about the performance?

As you can see we lost 10−3 order of magnitude but it is still a a good time for some millions of processes.

Conclusions

Criterion is a great tool, it even prepares a full report about your performance in html file that you find in the target folder. Here are some graphs it made.

So with that I could make a better code. Normally you just make a test and follow a TDD process to archive a better quality software, but I think that the search for a quality also pass by a optimized algorithm and tools like criterion make this goal much more easier to implement.

I hope you guys liked it and until next time!

Writing to make ourselves better programmers.