{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/zephir/issues/1163","repository_url":"https://api.github.com/repos/phalcon/zephir","labels_url":"https://api.github.com/repos/phalcon/zephir/issues/1163/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/zephir/issues/1163/comments","events_url":"https://api.github.com/repos/phalcon/zephir/issues/1163/events","html_url":"https://github.com/phalcon/zephir/issues/1163","id":125772023,"number":1163,"title":"Optimisation","user":{"login":"fezfez","id":1162307,"avatar_url":"https://avatars.githubusercontent.com/u/1162307?v=3","gravatar_id":"","url":"https://api.github.com/users/fezfez","html_url":"https://github.com/fezfez","followers_url":"https://api.github.com/users/fezfez/followers","following_url":"https://api.github.com/users/fezfez/following{/other_user}","gists_url":"https://api.github.com/users/fezfez/gists{/gist_id}","starred_url":"https://api.github.com/users/fezfez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fezfez/subscriptions","organizations_url":"https://api.github.com/users/fezfez/orgs","repos_url":"https://api.github.com/users/fezfez/repos","events_url":"https://api.github.com/users/fezfez/events{/privacy}","received_events_url":"https://api.github.com/users/fezfez/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-01-09T17:56:22Z","updated_at":"2016-04-11T21:24:29Z","closed_at":null,"body":"Hi,\r\nI made a little benchmark for comparing zephir/c/php by the following code.\r\n\r\n## Zephir\r\n\r\n```zephir\r\n\r\nnamespace Toto;\r\n\r\nclass Aclass\r\n{\r\n    public function write() -> void\r\n    {\r\n        int time, i;\r\n        var fp;\r\n        string header;\r\n        \r\n        let time = 3000000;\r\n        \r\n        let fp = fopen(\"header.csv\", \"w+\");\r\n        let header = \"fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez\";\r\n        \r\n        for i in range(1, time) {\r\n            fwrite(fp, header);\r\n        }\r\n        \r\n    }\r\n}\r\n```\r\n\r\n```bash\r\ntime php -d extension=/usr/lib/php5/20131226/toto.so testzep.php \r\n\r\nreal\t0m5.788s\r\nuser\t0m0.200s\r\nsys\t0m3.036s\r\n\r\n```\r\n\r\n## PHP\r\n\r\n```php\r\n$time = 3000000;\r\n$fp = fopen('header.csv', 'w+');\r\n$header = 'fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez';\r\n\r\nfor ($i = 0; $i < $time; $i++) {\r\n    fwrite($fp, $header);\r\n}\r\n```\r\n\r\n\r\n````bash\r\ntime php test.php\r\n\r\nreal\t0m4.675s\r\nuser\t0m0.468s\r\nsys\t0m3.288s\r\n\r\n```\r\n\r\n## C\r\n\r\n```c\r\n#include <stdio.h>\r\n\r\nmain() {\r\n   FILE *fp;\r\n   int a;\r\n\r\n   fp = fopen(\"test.txt\", \"w+\");\r\n   for( a = 1; a < 3000000; a = a + 1 ){\r\n       fputs(\"fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez\", fp);\r\n   }\r\n   fclose(fp);\r\n}\r\n```\r\n\r\n````bash\r\ntime ./test \r\n\r\nreal\t0m2.516s\r\nuser\t0m0.008s\r\nsys\t0m0.316s\r\n```\r\n\r\nZephir pretty bad ! But let's see the c code generated for the write method.\r\n\r\n```c\r\nPHP_METHOD(Toto_Aclass, write) {\r\n\r\n\tzend_bool _2;\r\n\tzval *header;\r\n\tzval *fp = NULL, _0, _1;\r\n\tint time, i, ZEPHIR_LAST_CALL_STATUS, _3, _4;\r\n\r\n\tZEPHIR_MM_GROW();\r\n\r\n\ttime = 3000000;\r\n\tZEPHIR_SINIT_VAR(_0);\r\n\tZVAL_STRING(&_0, \"header.csv\", 0);\r\n\tZEPHIR_SINIT_VAR(_1);\r\n\tZVAL_STRING(&_1, \"w+\", 0);\r\n\tZEPHIR_CALL_FUNCTION(&fp, \"fopen\", NULL, 1, &_0, &_1);\r\n\tzephir_check_call_status();\r\n\tZEPHIR_INIT_VAR(header);\r\n\tZVAL_STRING(header, \"fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez,fzefze,fezfze,fzefze,fzefze,fezfez\", 1);\r\n\t_4 = time;\r\n\t_3 = 1;\r\n\t_2 = 0;\r\n\tif (_3 <= _4) {\r\n\t\twhile (1) {\r\n\t\t\tif (_2) {\r\n\t\t\t\t_3++;\r\n\t\t\t\tif (!(_3 <= _4)) {\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\t_2 = 1;\r\n\t\t\t}\r\n\t\t\ti = _3;\r\n\t\t\tzephir_fwrite(NULL, fp, header TSRMLS_CC);\r\n\t\t}\r\n\t}\r\n\tZEPHIR_MM_RESTORE();\r\n\r\n}\r\n```\r\n\r\nWtf is this loops ??? After replace the loops by \r\n\r\n\r\n```c\r\n    for( i = 1; i < 3000000; i = i + 1 ){\r\n            zephir_fwrite(NULL, fp, header TSRMLS_CC);\r\n    }\r\n```\r\n\r\nRecompile by hand and rebenchmark\r\n\r\n```bash\r\ntime php -d extension=/usr/lib/php5/20131226/toto.so testzep.php \r\n\r\nreal\t0m3.212s\r\nuser\t0m0.208s\r\nsys\t0m2.968s\r\n\r\n```\r\n\r\nIs the zephir code optimized ? Or the generating code is very not optimized ?\r\n\r\nPHP 5.6.4-4ubuntu6.2 (cli) (built: Jul  2 2015 15:29:28) \r\nCopyright (c) 1997-2014 The PHP Group\r\nZend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies\r\n    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies\r\nZephir version 0.8.0a\r\n\r\n\r\n<bountysource-plugin>\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/29732368-optimisation?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F280146&utm_medium=issues&utm_source=github).\r\n</bountysource-plugin>"},"comment":{"url":"https://api.github.com/repos/phalcon/zephir/issues/comments/208568901","html_url":"https://github.com/phalcon/zephir/issues/1163#issuecomment-208568901","issue_url":"https://api.github.com/repos/phalcon/zephir/issues/1163","id":208568901,"user":{"login":"sergeyklay","id":1256298,"avatar_url":"https://avatars.githubusercontent.com/u/1256298?v=3","gravatar_id":"","url":"https://api.github.com/users/sergeyklay","html_url":"https://github.com/sergeyklay","followers_url":"https://api.github.com/users/sergeyklay/followers","following_url":"https://api.github.com/users/sergeyklay/following{/other_user}","gists_url":"https://api.github.com/users/sergeyklay/gists{/gist_id}","starred_url":"https://api.github.com/users/sergeyklay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sergeyklay/subscriptions","organizations_url":"https://api.github.com/users/sergeyklay/orgs","repos_url":"https://api.github.com/users/sergeyklay/repos","events_url":"https://api.github.com/users/sergeyklay/events{/privacy}","received_events_url":"https://api.github.com/users/sergeyklay/received_events","type":"User","site_admin":false},"created_at":"2016-04-11T21:24:28Z","updated_at":"2016-04-11T21:24:28Z","body":"So we have Phalcon Beanstalk component that usually slower than native  PHP"}}