From 7b00b3bb81cd816085cc15d8586ed0c898aecb38 Mon Sep 17 00:00:00 2001 From: Ari Archer Date: Thu, 20 Jan 2022 17:13:29 +0200 Subject: [PATCH] update @ Thu 20 Jan 17:13:29 EET 2022 Signed-off-by: Ari Archer --- .../Fast-atoi()-in-c-and-c++_-8139771109.html | 80 ++++++++++++ .../blogs/Fast-atoi-in-c(++)_8279372928.html | 116 ------------------ page/blog/index.html | 14 +-- page/blog/index.md | 2 +- 4 files changed, 88 insertions(+), 124 deletions(-) create mode 100644 page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html delete mode 100644 page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html diff --git a/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html b/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html new file mode 100644 index 0000000..138c94e --- /dev/null +++ b/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html @@ -0,0 +1,80 @@ + + + + + + + Ari::web -> Blog + + + + + +
+

Fast atoi() in C and C++

+

+ 2022-01-20 17:10:10 EET | + back + | + home + | + git +

+
+ +

+ std::atoi (in C++) is fast on its own but can be slow so + for stuff you need speed you can use this: +

+
    +
  • Option one
  • +
+
int fast_atoi(const char *int_string) {
+    int value = 0;
+
+    while (*int_string && *int_string >= '0' && *int_string <= '9')
+        value = value * 10 + (*int_string++ - '0');
+
+    return value;
+}
+
+

+ This requires no headers, just pure C (also valid in C++), you can put + this into your code and it will just work! +

+
    +
  • Option two
  • +
+
#include <assert.h>
+
+int fast_atoi(char *int_string) {
+    int value = 0;
+
+    while (*int_string) {
+        if (!(*int_string >= '0' && *int_string <= '9'))
+            assert(0 && "atoi(): invalid int_string");
+
+        value = value * 10 + (*int_string++ - '0');
+    }
+
+    return value;
+}
+
+

+ This option is throws an assertion error if it encounters an invalid + string but this requires assert.h as a dependency. +

+

+ In C++ assert.h should be replaced with + cassert. +

+
+ + + diff --git a/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html b/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html deleted file mode 100644 index dbfe048..0000000 --- a/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html +++ /dev/null @@ -1,116 +0,0 @@ - - - - - - - - Ari::web -> Blog - - - - - - -
-

- Fast atoi in C(++) -

-

- 2022-01-20 16:58:31 EET | - - back - - | - - home - - | - - git - -

-
-

- - std::atoi - - (in C++) is fast on its own but can be slow -so for stuff you need speed you can use this: -

-
    -
  • - Option one -
  • -
-

- ``` -int fast_atoi(const char *int_string) { - int value = 0; -

-
while (*int_string && *int_string >= '0' && *int_string <= '9')
-    value = value * 10 + (*int_string++ - '0');
-
-return value;
-
-

- } -``` -

-

- This requires no headers, just pure C (also valid in C++), -you can put this into your code and it will just work! -

-
    -
  • - Option two -
  • -
-

- ``` -

-

- include - - -

-

- int fast_atoi(char *int_string) { - int value = 0; -

-
while (*int_string) {
-    if (!(*int_string >= '0' && *int_string <= '9'))
-        assert(0 && "atoi(): invalid int_string");
-
-    value = value * 10 + (*int_string++ - '0');
-}
-
-return value;
-
-

- } -``` -

-

- This option is throws an assertion error if it encounters an -invalid string but this requires - - assert.h - - as a dependency. -

-

- In C++ - - assert.h - - should be replaced with - - cassert - - . -

-
- - - diff --git a/page/blog/index.html b/page/blog/index.html index 1c3dc3d..49dce9d 100644 --- a/page/blog/index.html +++ b/page/blog/index.html @@ -16,7 +16,7 @@ My blogs - Last updated on: 2022-01-20 16:58:31 EET + Last updated on: 2022-01-20 17:10:10 EET
diff --git a/page/blog/index.md b/page/blog/index.md index 7f65063..bb57f4a 100644 --- a/page/blog/index.md +++ b/page/blog/index.md @@ -21,4 +21,4 @@ * [New domain! -- ari-web.xyz](/page/blog/blogs/New-domain!----ari-web.xyz_7375640347.html) * [Funny thing about my site](/page/blog/blogs/Funny-thing-about-my-site_6342443591.html) * [Fixing kernel freezing on realtek WiFi](/page/blog/blogs/Fixing-kernel-freezing-on-realtek-wifi_5342268108.html) -* [Fast atoi in C(++)](/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html) \ No newline at end of file +* [Fast atoi() in C and C++](/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html) \ No newline at end of file