Skip to main content
deleted 2 characters in body
Source Link
JNS
  • 421
  • 2
  • 6

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ static const std::regex re{"^[a-zA-Z][a-zA-Z0-9_]+$"}; return std::regex_match(nom, re); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 36% the total time for mine.

With -O3, it was 2.5% of the total time for minethe regular expression. So the more specific approach seems to be more easily optimised by the compiler.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ static const std::regex re{"^[a-zA-Z][a-zA-Z0-9_]+$"}; return std::regex_match(nom, re); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ static const std::regex re{"^[a-zA-Z][a-zA-Z0-9_]+$"}; return std::regex_match(nom, re); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 6% the total time for mine.

With -O3, it was 2.5% of the total time for the regular expression. So the more specific approach seems to be more easily optimised by the compiler.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

deleted 60 characters in body
Source Link
JNS
  • 421
  • 2
  • 6

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ // Check if nom begins with alphabet followed by alphanumericstatic withconst '_'std::regex allowedre{"^[a-zA-Z][a-zA-Z0-9_]+$"}; return std::regex_match(nom, std::regex("^[a-zA-Z][a-zA-Z0-9_]+$"))re); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ // Check if nom begins with alphabet followed by alphanumeric with '_' allowed return std::regex_match(nom, std::regex("^[a-zA-Z][a-zA-Z0-9_]+$"))); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ static const std::regex re{"^[a-zA-Z][a-zA-Z0-9_]+$"}; return std::regex_match(nom, re); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

deleted 23 characters in body
Source Link
JNS
  • 421
  • 2
  • 6

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ // Check if nom begins with alphabet followed by alphanumeric with '_' allowed return std::regex_match(nom, std::regex("^[a-zA-Z][a-zA-Z0-9_]+$"))); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr std::stringchar* err4{"ERROR. Init again."}; 

where you are using threetwo modern C++ features: constant expressions, std::string and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ // Check if nom begins with alphabet followed by alphanumeric with '_' allowed return std::regex_match(nom, std::regex("^[a-zA-Z][a-zA-Z0-9_]+$"))); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr std::string err4{"ERROR. Init again."}; 

where you are using three modern C++ features: constant expressions, std::string and uniform initialisation.

Regular Expression Solution

Firstly, this can be accomplished with regular expressions rather easily:

bool ok_tret(const std::string& nom){ // Check if nom begins with alphabet followed by alphanumeric with '_' allowed return std::regex_match(nom, std::regex("^[a-zA-Z][a-zA-Z0-9_]+$"))); } 

The advantage is obvious: compactness. We are making a single call to a function, ok_tret is basically acting as a wrapper function.

The problem with this some might say is readability: not everyone understands regular expressions, but that can be said of other language features like template metaprogramming and moreover there are many tools online to build your own regular expressions.

Performance Considerations

I tested both the regular expression function and your function, for a million strings, of length 100 characters, randomly distributed. Your function was 3.5% the total time for mine.

This is to be expected since a specialised solution will run faster; regular expressions are coded to deal with virtually any matching conditions. However, I suspect this function won't be called as in my test conditions and the performance difference won't come into play.


Some obvious complaints with your code are not to use a const char*, go with,

constexpr char* err4{"ERROR. Init again."}; 

where you are using two modern C++ features: constant expressions and uniform initialisation.

added 542 characters in body
Source Link
JNS
  • 421
  • 2
  • 6
Loading
Source Link
JNS
  • 421
  • 2
  • 6
Loading