Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #11  
Old 04-19-2011, 01:58 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by YankForum View Post
nice one , but the most important question here is which one will be processed faster in php? in my opinion the second proposed code by boofo should be the fastest since it only takes 2 values true or false??? of course as lynne says the first one is more readable and easy to customize , i like it the most
nice thread , thanks boofo
I think the second code (the true or false) is actually easier to read, in my opinion. I still can't get uses to the idea of not using brackets (like in Lynne's code) around if statements. It just seems to be open to me and not finished, even though it is, if that makes any sense.

Quote:
Originally Posted by Disasterpiece View Post
in php you have automatic typecast, so everything evalueates to either true or false. Zero (numeric) evaluates to false, so the ! inverts the evaluation to "true" and therefore you set the totalthreads to 1. It's just that it looks nicer because it's short :P
if it takes one or more additional cycles to process it? maybe. But it's php after all, so the uber-performance isn't that of an issue.

max is a mathematic function which returns the greater value of the given params. If you don't want to have a number which is negative or equal to 0, you use max($n, 1) so you guarantee that you always have at least 1.
Not sure about the performance, It's possible that the php engine uses the native c math function, so this might even speed things up a bit? dunno but then again, doesn't really matter :P

Just intended to brag with my mad php skillz, honestly I think the idea itself is an issue here (it throws an error when it's zero? well, just increase it then. <- wtf?)
If you really want to get help, you should explain your problem instead of the solution


PHP Code:
if ($vbulletin->options['something_here'] == ''$comma_separated '0'
I have nothing to add. maybe use "empty()" instead of testing for empty string, since the empty function checks for null as well and doesn't throw a warning when the value is not set.
There is no problem. Just curious as to what other coders think.
Reply With Quote
  #12  
Old 04-19-2011, 03:03 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

If you're going for shortest code, how about:
PHP Code:
$totalthreads += !$totalthreads;
$totalposts += !$totalposts


Anyway, I'm not a php expert but I think I'd go for readability unless it's in a critical area where you're specifically worried about the speed. If the point is to make sure $totalthreads and $totalposts are a positive number, I'd probably go with
PHP Code:
if ($totalthreads 1)
{
    
$totalthreads 1;
}
if (
$totalposts 1)
{
    
$totalposts 1;


even if it's not "supposed" to ever be less than 0. (ETA: or on second thought maybe using max() would make the intention more obvious).
Reply With Quote
  #13  
Old 04-19-2011, 03:25 PM
Disasterpiece's Avatar
Disasterpiece Disasterpiece is offline
 
Join Date: Apr 2007
Location: GER
Posts: 765
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
If you're going for shortest code, how about:
PHP Code:
$totalthreads += !$totalthreads;
$totalposts += !$totalposts
damn, you got me there

Altough I could argue, that the addition is always called and therefore produces more cycles then a comparison :P

However, this is way too little code to actually perform "real" optimization. In the end, it's the coders preferrence which is important and easier to read for himself. There are more grave issues of coding-crimes where I sometimes like to kick the developer in the face; how if/else and brackets are placed doesn't belong in this category tough.
Reply With Quote
  #14  
Old 04-19-2011, 03:30 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
If you're going for shortest code, how about:
PHP Code:
$totalthreads += !$totalthreads;
$totalposts += !$totalposts


Anyway, I'm not a php expert but I think I'd go for readability unless it's in a critical area where you're specifically worried about the speed. If the point is to make sure $totalthreads and $totalposts are a positive number, I'd probably go with
PHP Code:
if ($totalthreads 1)
{
    
$totalthreads 1;
}
if (
$totalposts 1)
{
    
$totalposts 1;


even if it's not "supposed" to ever be less than 0. (ETA: or on second thought maybe using max() would make the intention more obvious).
But which was would be the fastest and less intensive?
Reply With Quote
  #15  
Old 04-19-2011, 03:43 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Disasterpiece View Post
Altough I could argue, that the addition is always called and therefore produces more cycles then a comparison :P
I agree.

Quote:
... how if/else and brackets are placed doesn't belong in this category tough.
I also agree with that - I didn't mean to advocate any specific style. I guess my feeling about Boofo's second example in the OP is that, while it's isn't incorrect, my preference is to use the trinary (.. ? .. : ..) operator in places where you are interested in the resulting value, and the use of it for the "side effects" makes it a little more difficult to read and probably doesn't improve the efficiency. (Which I guess is pretty much what Lynne already said).

Quote:
Originally Posted by Boofo View Post
But which was would be the fastest and less intensive?
That I don't know anything about, for php at least.
Reply With Quote
  #16  
Old 04-19-2011, 04:03 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

vb 4 use max in quite a few places in the code, which I had never noticed before.

It all really boils down to what we get used to in coding styles, I guess. There are always newer and better ways to do things, so change is inevitable.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:08 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.08358 seconds
  • Memory Usage 2,239KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (6)bbcode_php
  • (7)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete