Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
How does strlen function works in c?

The source code I found for strlen function in c is the one in below:

int
strlen(string)
    char *string;       /* String whose length is wanted. */
{
    register char *p = string;

    while (1) {
    if (p[0] == 0) {
        return p - string;
    }
    if (p[1] == 0) {
        return p + 1 - string;
    }
    if (p[2] == 0) {
        return p + 2 - string;
    }
    if (p[3] == 0) {
        return p + 3 - string;
    }
    p += 4;
    }
}

The link is this. I don't understand why the steps are 4. The function jumps 4 bytes by 4 bytes over the string. Why it does this way? It could be implemented like this:

int
strlen(string)
    char *string;       /* String whose length is wanted. */
{
    register char *p = string;

    while (1) {
    if (*p == 0)
        return p - string;
    p += 1;
    }
}

Is there any performance related reason behind it?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

This is called "loop unrolling" and is done purely for performance reasons. Modern compilers are perfectly good at doing their own loop unrolling, so doing it manually is likely going to hurt performance relative to the compiler's generated code.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda