update @ Thu 20 Jan 17:13:29 EET 2022

Signed-off-by: Ari Archer <truncateddinosour@gmail.com>
This commit is contained in:
Ari Archer 2022-01-20 17:13:29 +02:00
parent e07bc9774b
commit 7b00b3bb81
4 changed files with 88 additions and 124 deletions

View file

@ -0,0 +1,80 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<title>Ari::web -&gt; Blog</title>
<meta
content="website webdev linux programming ari terminal dark blog javascript opensource free"
name="keywords"
/>
<meta
content="Blog on 2022-01-20 17:10:10 EET - Fast atoi() in C and C++"
name="description"
/>
<meta content="follow" name="robots" />
</head>
<body>
<div>
<h1>Fast atoi() in C and C++</h1>
<p>
2022-01-20 17:10:10 EET |
<a href=".."> back </a>
|
<a href="/"> home </a>
|
<a href="/git"> git </a>
</p>
<hr />
<p>
<code>std::atoi</code> (in C++) is fast on its own but can be slow so
for stuff you need speed you can use this:
</p>
<ul>
<li>Option one</li>
</ul>
<pre><code><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">fast_atoi</span>(<span class="hljs-params"><span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *int_string</span>) </span>{
<span class="hljs-keyword">int</span> <span class="hljs-keyword">value</span> = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (*int_string &amp;&amp; *int_string &gt;= <span class="hljs-string">'0'</span> &amp;&amp; *int_string &lt;= <span class="hljs-string">'9'</span>)
<span class="hljs-keyword">value</span> = <span class="hljs-keyword">value</span> * <span class="hljs-number">10</span> + (*int_string++ - <span class="hljs-string">'0'</span>);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">value</span>;
}
</code></pre>
<p>
This requires no headers, just pure C (also valid in C++), you can put
this into your code and it will just work!
</p>
<ul>
<li>Option two</li>
</ul>
<pre><code><span class="hljs-meta">#include &lt;assert.h&gt;</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">fast_atoi</span>(<span class="hljs-params"><span class="hljs-keyword">char</span> *int_string</span>) </span>{
<span class="hljs-keyword">int</span> <span class="hljs-keyword">value</span> = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (*int_string) {
<span class="hljs-keyword">if</span> (!(*int_string &gt;= <span class="hljs-string">'0'</span> &amp;&amp; *int_string &lt;= <span class="hljs-string">'9'</span>))
assert(<span class="hljs-number">0</span> &amp;&amp; <span class="hljs-string">"atoi(): invalid int_string"</span>);
<span class="hljs-keyword">value</span> = <span class="hljs-keyword">value</span> * <span class="hljs-number">10</span> + (*int_string++ - <span class="hljs-string">'0'</span>);
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">value</span>;
}
</code></pre>
<p>
This option is throws an assertion error if it encounters an invalid
string but this requires <code>assert.h</code> as a dependency.
</p>
<p>
In C++ <code>assert.h</code> should be replaced with
<code>cassert</code>.
</p>
</div>
</body>
</html>
<!-- this is automatically generated by scripts/add_blog -->

View file

@ -1,116 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>
Ari::web -&gt; Blog
</title>
<meta content="website webdev linux programming ari terminal dark blog javascript opensource free" name="keywords"/>
<meta content="Blog on 2022-01-20 16:58:31 EET - Fast atoi in C(++)" name="description"/>
<meta content="follow" name="robots"/>
</head>
<body>
<div>
<h1>
Fast atoi in C(++)
</h1>
<p>
2022-01-20 16:58:31 EET |
<a href="..">
back
</a>
|
<a href="/">
home
</a>
|
<a href="/git">
git
</a>
</p>
<hr/>
<p>
<code>
std::atoi
</code>
(in C++) is fast on its own but can be slow
so for stuff you need speed you can use this:
</p>
<ul>
<li>
Option one
</li>
</ul>
<p>
```
int fast_atoi(const char *int_string) {
int value = 0;
</p>
<pre><code>while (*int_string &amp;&amp; *int_string &gt;= '0' &amp;&amp; *int_string &lt;= '9')
value = value * 10 + (*int_string++ - '0');
return value;
</code></pre>
<p>
}
```
</p>
<p>
This requires no headers, just pure C (also valid in C++),
you can put this into your code and it will just work!
</p>
<ul>
<li>
Option two
</li>
</ul>
<p>
```
</p>
<h1>
include
<assert.h>
</assert.h>
</h1>
<p>
int fast_atoi(char *int_string) {
int value = 0;
</p>
<pre><code>while (*int_string) {
if (!(*int_string &gt;= '0' &amp;&amp; *int_string &lt;= '9'))
assert(0 &amp;&amp; "atoi(): invalid int_string");
value = value * 10 + (*int_string++ - '0');
}
return value;
</code></pre>
<p>
}
```
</p>
<p>
This option is throws an assertion error if it encounters an
invalid string but this requires
<code>
assert.h
</code>
as a dependency.
</p>
<p>
In C++
<code>
assert.h
</code>
should be replaced with
<code>
cassert
</code>
.
</p>
</div>
</body>
</html>
<!-- this is automatically generated by scripts/add_blog -->

View file

@ -16,7 +16,7 @@
My blogs
</h1>
<code>
Last updated on: 2022-01-20 16:58:31 EET
Last updated on: 2022-01-20 17:10:10 EET
</code>
<div>
<ul>
@ -136,8 +136,8 @@
</a>
</li>
<li>
<a href="/page/blog/blogs/Fast-atoi-in-c(++)_8279372928.html">
Fast atoi in C(++)
<a href="/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html">
Fast atoi() in C and C++
</a>
</li>
</ul>

View file

@ -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)
* [Fast atoi() in C and C++](/page/blog/blogs/Fast-atoi()-in-c-and-c++_-8139771109.html)