在R中的整数中查找最低有效置位的最快/最有效方法是什么?

时间:2019-01-19 12:54:04

标签: r optimization bit-manipulation bitwise-operators

对于R,我有一个类似于What's the fastest method to return the position of the least significant bit set in an integer in Python 3?finding the first set bit in a binary numberPosition of least significant bit that is set的问题。我需要找到R中整数中最低有效位的位置。 Spätzle提出的建议如下:

unlist(lapply(x, function(z) min(which(as.integer(intToBits(z)) == 1))-1))

有更有效的方法吗?

2 个答案:

答案 0 :(得分:4)

以下内容更快:

public class SelectedMediaViewPagerAdapter extends PagerAdapter {
private final Context context;
private final LayoutInflater layoutInflater;
public ArrayList<MediaItem> mediaItems;
PlayerView playerView;
SimpleExoPlayer player;

public String path = "";

public SelectedMediaViewPagerAdapter(Context context, ArrayList<MediaItem> mediaItems) {

    this.mediaItems = mediaItems;
    this.context = context;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DefaultTrackSelector trackSelector = new DefaultTrackSelector();
    player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);

}

@Override
public int getCount() {
    return mediaItems.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view == object);
}

@Override
public Object instantiateItem(final ViewGroup container, int position) {
    RelativeLayout layout = (RelativeLayout) layoutInflater.inflate(R.layout.item_selected_media_view_pager, container, false);
    RelativeLayout vidView= layout.findViewById(R.id.vidView);
    ImageView imgView = layout.findViewById(R.id.imgView);
    if (mediaItems.get(position).isVideo()) {
        imgView.setVisibility(View.GONE);
        vidView.setVisibility(View.VISIBLE);
        String video_url;
        playerView = layout.findViewById(R.id.player_view);
        final ProgressBar progressBar = layout.findViewById(R.id.progresbar_video_play);
        progressBar.setVisibility(View.VISIBLE);

        playerView.setPlayer(player);

        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, context.getResources().getString(R.string.app_name)));
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(mediaItems.get(position).getPath()));
        // Prepare the player with the source.
        player.prepare(videoSource);
        player.setPlayWhenReady(false);
        player.addListener(new Player.DefaultEventListener() {
            @Override
            public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
                if (playWhenReady) {
                    progressBar.setVisibility(View.GONE);
                }
                super.onPlayerStateChanged(playWhenReady, playbackState);
            }
        });
    }else {
        // path = drawable.get(drawable.size() - position - 1);
        imgView.setVisibility(View.VISIBLE);
        vidView.setVisibility(View.GONE);
        Glide.with(context).load("file://" + mediaItems.get(position).getPath()).into(imgView);
    }
    container.addView(layout);
    return layout;
}



@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
}

为进行比较:

f <- function(x){
  log2(bitwAnd(x,-x))
}

快速测试:

g <- function(x){
  unlist(lapply(x, function(z) min(which(as.integer(intToBits(z)) == 1))-1))
}

答案 1 :(得分:3)

如果向量较长且想使用C ++,那么以下代码可能会为您提供帮助(以及<!DOCTYPE html> <html> <body> <h2>JavaScript Loops</h2> <p id="demo"></p> <script> var i; elmt=[] elmt[0] = 0; elmt[1] = 1; elmt[2] = 2; elmt[3] = 3; elmt[4] = 4; elmt[5] = 7; elmt[6] = 8; elmt[7] = 9; elmt[8] = 10; elmt[9] = 11; var sum = 0; for (let x of elmt) { sum += x; } var avg = sum/elmt.length; document.write( "The sum of all the elements is: " + sum + " The average is: " + avg ); </script> </body> </html> Rcpp中的ffs函数):

strings.h

将上面的代码另存为文件,例如#include <Rcpp.h> #include <strings.h> using namespace Rcpp; // [[Rcpp::export]] Rcpp::IntegerVector lsb(const IntegerVector x) { IntegerVector res(x.size()); std::transform(x.begin(), x.end(), res.begin(), ffs); return(res-1); # To start from 0 } ,并使用lsb.cpp包中的sourceCpp("lsb.cpp")对其进行编译。

速度稍快-至少对于更长的输入矢量而言,开销可以忽略不计

Rcpp