Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPTIMIZE: replace read with printf #181

Open
3 tasks done
andyneff opened this issue Oct 30, 2019 · 2 comments
Open
3 tasks done

OPTIMIZE: replace read with printf #181

andyneff opened this issue Oct 30, 2019 · 2 comments

Comments

@andyneff
Copy link
Member

andyneff commented Oct 30, 2019

printf -v var_name '%s' "${value}" # %s is important here to prevent escaping

appears to be 400% faster than

read -r -d '' var_name <<< "${value}"

If there are no side effects, everything that can and uses read and can be replaced with printf

  • Verify speed on mac
  • Verify speed on windows
  • Verify speed on linux
@andyneff
Copy link
Member Author

andyneff commented Jul 30, 2020

Does not work for arrays until bash 4.1

printf -v x[0] '%s' foo

@andyneff
Copy link
Member Author

andyneff commented Jul 30, 2020

100x faster on windows for variable
2.5x faster on windows for array element
2x faster on linux for array elements
8x faster on linux for variables
5x faster on darling (only works for variables)
20x faster on macOS bash 3.2 (only works for variables)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant