{"action":"created","issue":{"url":"https://api.github.com/repos/crystal-lang/crystal/issues/5029","repository_url":"https://api.github.com/repos/crystal-lang/crystal","labels_url":"https://api.github.com/repos/crystal-lang/crystal/issues/5029/labels{/name}","comments_url":"https://api.github.com/repos/crystal-lang/crystal/issues/5029/comments","events_url":"https://api.github.com/repos/crystal-lang/crystal/issues/5029/events","html_url":"https://github.com/crystal-lang/crystal/issues/5029","id":260075948,"number":5029,"title":"File#each_char performance ","user":{"login":"monouser7dig","id":26345163,"avatar_url":"https://avatars2.githubusercontent.com/u/26345163?v=4","gravatar_id":"","url":"https://api.github.com/users/monouser7dig","html_url":"https://github.com/monouser7dig","followers_url":"https://api.github.com/users/monouser7dig/followers","following_url":"https://api.github.com/users/monouser7dig/following{/other_user}","gists_url":"https://api.github.com/users/monouser7dig/gists{/gist_id}","starred_url":"https://api.github.com/users/monouser7dig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monouser7dig/subscriptions","organizations_url":"https://api.github.com/users/monouser7dig/orgs","repos_url":"https://api.github.com/users/monouser7dig/repos","events_url":"https://api.github.com/users/monouser7dig/events{/privacy}","received_events_url":"https://api.github.com/users/monouser7dig/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2017-09-24T11:03:19Z","updated_at":"2017-09-24T14:27:24Z","closed_at":null,"author_association":"NONE","body":"I wrote following code in order to simulate a automata (this is a condensed version which shows the same issue)\r\n\r\n```Crystal\r\nf = File.open(\"./my_file\", mode = \"r\")\r\n\r\ncount = 0\r\n\r\nh_map = {'U' => 0,\r\n         'D' => 1,\r\n         'A' => 2,\r\n         'Z' => 3,\r\n         'R' => 4,\r\n}\r\n\r\nf.each_char do |val|\r\n  next if val == '\\n'\r\n  count += h_map[val]\r\nend\r\n\r\np count\r\n```\r\n\r\nwhere `my_file` is something like \r\n```\r\nUDRAZRAZAZRRAZURURURAZAZRRAZARRRZARZRRRAZAZRZAZARRZAZDAZAZUDURAZDRURAZRAZRRAZ\r\nAZUUDRAZUDUARZUAZARRZ\r\nAZRUAZUAZUAZDAZUARRRRRZARZDAZARZARRRZUARZAZDUAZARRZRAZARZAZRRRRDDARRZRARZAZAZAZUDRRAZARZARZAZUAZARZUARRZDARRZRUDDUAZURRDRARRZDARZARZRAZDARZARZAZUARRZURARZARZUDARZAZARZARRZUARRZRARZAZAZAZARZARZDARZUDAZAZUDARZARRZARZAZAZAZUDUARRRZAZRRAZDRURA\r\n```\r\nbut about 8GB of that\r\n\r\nI have a C implementation which just loads all in RAM and takes under 20s\r\n\r\nthis crystal one takes about 2min though\r\n\r\n```\r\nreal\t1m58,173s\r\nuser\t1m49,195s\r\nsys\t0m6,172s\r\n```\r\n\r\nhow would I make the file read speed better? (I compiled using --release)\r\nthanks "},"comment":{"url":"https://api.github.com/repos/crystal-lang/crystal/issues/comments/331713588","html_url":"https://github.com/crystal-lang/crystal/issues/5029#issuecomment-331713588","issue_url":"https://api.github.com/repos/crystal-lang/crystal/issues/5029","id":331713588,"user":{"login":"monouser7dig","id":26345163,"avatar_url":"https://avatars2.githubusercontent.com/u/26345163?v=4","gravatar_id":"","url":"https://api.github.com/users/monouser7dig","html_url":"https://github.com/monouser7dig","followers_url":"https://api.github.com/users/monouser7dig/followers","following_url":"https://api.github.com/users/monouser7dig/following{/other_user}","gists_url":"https://api.github.com/users/monouser7dig/gists{/gist_id}","starred_url":"https://api.github.com/users/monouser7dig/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/monouser7dig/subscriptions","organizations_url":"https://api.github.com/users/monouser7dig/orgs","repos_url":"https://api.github.com/users/monouser7dig/repos","events_url":"https://api.github.com/users/monouser7dig/events{/privacy}","received_events_url":"https://api.github.com/users/monouser7dig/received_events","type":"User","site_admin":false},"created_at":"2017-09-24T14:27:24Z","updated_at":"2017-09-24T14:27:24Z","author_association":"NONE","body":"here is the c code (which is not really my own but was given to me in some reduced form for an exercise)\r\n```C\r\nint results[9] = {1,1,1,1,1,1,1,1,0};\r\nint deltas[9][5] = {\r\n    {1,8,4,8,0}, //q0 \r\n    {2,0,5,8,1}, //q1\r\n    {3,1,6,8,2}, //q2\r\n    {8,2,7,8,3}, //q3\r\n\r\n    {8,8,8,0,4}, //q4\r\n    {8,8,8,1,5}, //q5\r\n    {8,8,8,2,6}, //q6\r\n    {8,8,8,3,7}, //q7\r\n\r\n    {8,8,8,8,8}, //q8\r\n};\r\n\r\nint result_f(char* w){\r\n    int sol;\r\n    int pointer = 0;\r\n    double counter;\r\n    counter = 0;\r\n    while (*w) {\r\n        pointer = deltas[pointer] [*w++ -'0'];\r\n        counter++;\r\n    }\r\n    sol = results[pointer];\r\n    printf(\"%d\\n\", sol);\r\n    printf(\"%f\\n\", counter);\r\n    return results [pointer];\r\n}\r\n```\r\nthe function is called with a pointer that points to the beginn of the buffer which is located in RAM using `fopen` and `malloc`\r\n\r\nasterite I will look at your version/ try and report back "}}
{"actor":{"display_login":"monouser7dig"}}